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.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

根据x,将小于x的节点放在大于x节点的前面。。

正常思路就行:新建两个链表,一个存放大于x的,一个存放小于x的。最后连接起来。注意写代码的一些细节。最后节点(大链表的最后一个)的next要设为null

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null||head.next==null) return head;
ListNode small=new ListNode(0);
ListNode big=new ListNode(0);
ListNode s=small,b=big; //用来遍历添加节点
while(head!=null){
if(head.val>=x){
b.next=head;
b=b.next;
}else{
s.next=head;
s=s.next;
}
head=head.next;
}
b.next=null; //大的最后一个节点的next要设为null,这一步一定不能忘记。不然它还是会指向原来的next。
s.next=big.next;
return small.next;
}
}

partition List(划分链表)的更多相关文章

  1. [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 ...

  2. [LeetCode] Partition List 划分链表

    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 划分链表

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

  4. 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 ...

  5. [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 ...

  6. 086 Partition List 分隔链表

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前.你应当保留两个分区中每个节点的初始相对位置.例如,给定1->4->3->2-&g ...

  7. Partition List(链表的插入和删除操作,找前驱节点)

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

  8. Leetcode86. Partition List分隔链表(双指针)

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...

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

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

随机推荐

  1. Java并发框架——AQS超时机制

    AQS框架提供的另外一个优秀机制是锁获取超时的支持,当大量线程对某一锁竞争时可能导致某些线程在很长一段时间都获取不了锁,在某些场景下可能希望如果线程在一段时间内不能成功获取锁就取消对该锁的等待以提高性 ...

  2. 深入剖析Tomcat类加载机制

    1JVM类加载机制 JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使用n ...

  3. unix os下du df简单用法

    转自:http://dadoneo.iteye.com/blog/984963 du命令参数详解见:http://baike.baidu.com/view/43913.htm 下面我们只对其做简单介绍 ...

  4. [python] Start a http server

    If you want to start a simple httpserver on your windows, you may choose python.simpleHTTPServer mod ...

  5. ROS_Kinetic_09 ROS基础内容(四)

    ROS_Kinetic_09 ROS基础内容(四) 参考网址: http://wiki.ros.org/cn/ROS/Tutorials/UsingRosEd http://wiki.ros.org/ ...

  6. TCP的定时器系列 — 超时重传定时器

    主要内容:TCP定时器概述,超时重传定时器.ER延迟定时器.PTO定时器的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd Q:一条TCP连接会使用 ...

  7. Ext JS 5初探(一)

    在开始前,先安装好Sencha Cmd 5.然后输入以下命令创建一个测试用的应用程序: sencha -sdk c:\ext5 generate app TestExt5 C:\TestExt5 想不 ...

  8. Linux学习笔记 --服务器优化

    Linux服务器优化 序言: 服务器操作建议 1.严格按照目录规范操作服务器 2.远程服务器不允许关机 3.不要在服务器访问高峰运行高负载命令 4.远程配置防火墙时,不要把自己踢出服务器 一.禁用不必 ...

  9. 【Android 应用开发】 Ubuntu 安装 Android Studio (旧版本|仅作参考)

    . 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...

  10. Android+Eclipse修改包路径

    在开发过程中发现之前定的包名或是路径不太合理,怎么修改呢?选中要修改的包,按F2按键,如下图: 图1 上图是我修改后的,修改前的包名是com.example.appcenter,自改为com.exam ...