Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

题意:

给定一个链表和一个值,把小于等于和大于该值的部分分别放到链表的一前一后去。

思路:

先分为两个链表

然后合并

代码:

 class Solution {
public ListNode partition(ListNode head, int x) {
ListNode leftDummy = new ListNode(-1);
ListNode rightDummy = new ListNode (-1);
ListNode left_cur = leftDummy;
ListNode right_cur = rightDummy;
ListNode cur = head; while( cur != null){
if(cur.val < x){
left_cur.next = cur;
left_cur = cur;
}else{
right_cur.next = cur;
right_cur = cur;
}
cur = cur.next;
}
left_cur.next = rightDummy.next;
right_cur.next = null;
return leftDummy.next;
}

[leetcode]86. Partition List划分链表的更多相关文章

  1. [LeetCode] 86. Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. LeetCode 86. Partition List 划分链表 C++

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. [LeetCode]86. Partition List分离链表

    /* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...

  4. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  6. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  7. leetcode 86. Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  8. partition List(划分链表)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  9. [LeetCode] 86. Partition List 解题思路

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

随机推荐

  1. xamarin C# 安卓实现 ListView 放大缩小

    翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java u ...

  2. docker lamp

    可以直接使用官方镜像搭建LAMP环境从官方下载PHP+Apache镜像和MySQL两个镜像来组成(如:php:7.2.3-apache-stretch和mysql:5.7.21)docker pull ...

  3. zombodb sql functions 说明

    zombodb 提供了好多方便的sql 函数工具类以及帮助函数 查看zombodb 版本zdb.version() select * from zdb.version(); version ----- ...

  4. [转]Java泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  5. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  6. Eclipse配置Python的IDE

    我第一个用来实际应用的编程语言是Java,于是对Eclipse情有独钟.但是自从上手了Notepad++后,使用Eclipse的机会越来越少. 最近开始学习Python,因为对Python不太熟悉,有 ...

  7. Java构造器练习题

    仔细阅读下面的程序 public class Car { String name = "汽车"; public Car(String name) { this.name = nam ...

  8. Vue中table合并单元格用法

    <table> <tr> <th>地名</th> <th>结果</th> <th>人名</th> < ...

  9. MySQL - 用户变量

    MySQL用户变量:基于会话变量实现的,可以暂存值,并传递给同一连接里的下一条sql使用的变量,当客户端连接退出时,变量会被释放. MySQL用户变量应用场景:同一连接,未关闭情况下,帮你暂存一些计算 ...

  10. 遇到问题或bug时要做的事。

    1,做事细心,只有细心才能减少bug量,做总结. 2,开发中遇到bug和错误,第一要想到是程序代码的问题.而首先想到的不是其他问题(比如版本,框架或兼容问题等). 3,程序不能按照自己的意愿执行,时先 ...