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. quartz 时间设置(定时任务scheduler)

    quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...

  2. Android Studio科普篇——2.误区

    1.删除一行是ctrl+x? 这是一个被谣传得很广泛的快捷键,但其实删除一行的快捷键不是ctrl+x,而是ctrl+y.ctrl+x在不选中内容的情况下,是剪切当前行,而ctrl+y才是删除当前行,它 ...

  3. WebLogic11g-集群相关概念

    weblogic集群架构相关的概念有:  1.服务器(server,控制台选择环境-服务器)  2.集群(cluster,控制台选择环境-集群)  3.计算机(machine,控制台选择环境-计算机) ...

  4. Java-IO之FilterInputStream和FilterOuptStream

    FilterInputStream的作用是用来封装其他的输入流,并为它们提供了额外的功能,它的常用的子类有BufferedInputStream和DataInputStream.FilterOutpu ...

  5. 管道模式——pipeline与valve

    在一个比较复杂的大型系统中,假如存在某个对象或数据流需要被进行繁杂的逻辑处理的话,我们可以选择在一个大的组件中进行这些繁杂的逻辑处理,这种方式确实达到了目的,但却是简单粗暴的.或许在某些情况这种简单粗 ...

  6. Dynamics CRM2013 任务列表添加自定义按钮

    任务列表的command bar 上面添加自定义按钮如下 要注意的是此处的列表不是任务实体而是活动实体,如果你是在任务实体的home栏上面加那你永远看不见按钮的显示,但如果是要在任务的表单界面上加按钮 ...

  7. Socket编程实践(3) --Socket API

    socket函数 #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, ...

  8. NSData 与 struct 以及XML的转换。

    在做OC与C++ 混编的时候,我们可能会用到struct 与NSData的相互转换.在这里做一个记录 1.struct转换为NSData 例如如下的struct: struct tagPackageH ...

  9. css3的样式讲解-css学习之旅(3)

    css背景 属性:background-color:background-image:url("位置"):background-position:right等,px,百分数:bac ...

  10. Leetcode_100_Same Tree

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42061529 Given two binary trees ...