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. 分享:带波形的语音播放工具(wavesurfer-js)

    项目名称:wavesurfer-js github地址:https://github.com/katspaugh/wavesurfer.js 官网地址:http://wavesurfer-js.org ...

  2. VS中监视窗口,即时窗口和输出窗口的使用

    一.监视窗口 1.配置应用程序,使应用程序处于调试状态. 2.点击“调试”----“窗口”----“监视”----“监视1”,打开监视窗口. 3.在监视窗口中“名称”栏中输入变量名称或html元素id ...

  3. 某PHP代码加密

    <?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...

  4. Web性能压力测试之Webbench使用详解

    Webbench是知名的网站压力测试工具,它是由Lionbridge公司(http://www.lionbridge.com)开发.Webbench能测试处在相同硬件上,不同服务的性能以及不同硬件上同 ...

  5. 关于ASP.NET控件方面的学习(恢复版)

    前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...

  6. 第一次用Github desktop(mac)提交代码遇到的问题

    1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...

  7. [转]dos命令 cd命令使用说明[图文说明]

    Cddir(change directory,可以缩写为cd),其功能是显示当前目录的名称,或更改当前的目录. 应用时公带一个驱动器号(如: cd c:)在命令行cmd中输入 cd /? 可显示帮助信 ...

  8. java 迭代器iterator

    对于如ArrayList<E>类的数据,常用iterator遍历. ArrayList<String> list = new ArrayList<String>() ...

  9. 确认(confirm 消息对话框)

    confirm 消息对话框通常用于允许用户做选择的动作(包括一个确定按钮和一个取消按钮). 语法: confirm(str) str:在消息对话框中要显示的文本 返回值: 当用户点击"确定& ...

  10. Java线程(学习整理)--3--简单的死锁例子

    1.线程死锁的概念: 简单地理解下吧! 我们都知道,线程在执行的过程中是占着CPU的资源的,当多个线程都需要一个被锁住的条件才能结束的时候,死锁就产生了! 还有一个经典的死锁现象: 经典的“哲学家就餐 ...