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值大的元素,记录其前驱节点ptr,然后遍历单链表,大于x的跳过,小于x的接到ptr的后面,然后ptr=ptr.next。

   public ListNode partition(ListNode head, int x) {
if (head==null||head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode ptr = dummy;
ListNode pre = dummy;
while(ptr!=null&&ptr.next!=null&&ptr.next.val<x){
ptr=ptr.next;
pre=pre.next;
}
while(pre.next!=null){
ListNode tmp = pre.next;
if(pre.next.val<x){
ListNode curr = pre.next;
pre.next=curr.next;
curr.next=ptr.next;
ptr.next=curr;
ptr=ptr.next;
}
pre = tmp;
}
return dummy.next;
}

Partition List ——LeetCode的更多相关文章

  1. Partition List -- LeetCode

    原题链接: http://oj.leetcode.com/problems/partition-list/  这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面.我们仍然是使用链表最经常使用 ...

  2. Partition List leetcode java

    题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...

  3. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  7. [LeetCode] Partition List 划分链表

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

  8. [LeetCode]题解(python):086 - Partition List

    题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...

  9. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

随机推荐

  1. java.util.Random深入理解

    java.util.Random next方法的原理 比较好的参考文档: http://isky001.iteye.com/blog/1339979 package random.utilrandom ...

  2. Android EditText自动弹出输入法焦点

    http://mobile.51cto.com/aprogram-403138.htm 1. 看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的 ...

  3. oracle 查看用户表数目,表大小,视图数目等

    查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * fr ...

  4. strut2.xml中result param详细设置

    1.Struts2.xml配置文件: 2.Jsp中:说明回调函数一个参数即可.把上面的俩个参数msg和page封装到一起了 3.msg是Action中全局变量 可参考:http://qiaolevip ...

  5. left join 和 left outer join 有什么区别?

    left join 是left outer join的简写,left join默认是outer属性的.outer join则会返回每个满足第一个(顶端)输入与第二个(底端)输入的联接的行.它还返回任何 ...

  6. java项目测试log4j

    .literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...

  7. [LeetCode OJ] Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. nginx location

    1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何前缀:最大前缀匹配,举例如 ...

  9. Thinkphp 事物问题

    $m=D('YourModel');//或者是M(); $m2=D('YouModel2'); $m->startTrans();//在第一个模型里启用就可以了,或者第二个也行 $result= ...

  10. 《Python 二三事》——python学习必看(转载)

        面向初学者介绍Python相关的一些工具,以及可能遇到的常见问题. 原文出处 原文作者:八八年出生的男性,互联网上常用id是 jagttt .目前正从事 IT 行业的工作.业余爱好是动漫游加电 ...