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小的节点拆下来按顺序链接到一起构成list1,然后把剩下的节点按顺序链接到一起构成list2。最后list1.next=list2即可。代码如下:

 public class Solution {
public ListNode partition(ListNode head, int x) {
if(head==null || head.next==null) return head; ListNode Partition = head; ListNode nhead = new ListNode(0);
nhead.next = null;
ListNode ntail = nhead; ListNode nhead2 = new ListNode(0);
nhead2.next = null;
ListNode ntail2 = nhead2; ListNode p = null;
while(Partition!=null){
if(Partition.val < x){
p = Partition.next;
ntail.next = Partition;
ntail = ntail.next;
ntail.next = null;
Partition = p;
}
else{
p = Partition.next;
ntail2.next = Partition;
ntail2 = ntail2.next;
ntail2.next = null;
Partition = p;
}
}
ntail.next = nhead2.next;
return nhead.next;
}
}

LeetCode OJ 86. Partition List的更多相关文章

  1. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 【一天一道LeetCode】#86. Partition List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【Leetcode】86. Partition List

    Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...

  4. LeetCode OJ:Partition List(分割链表)

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

  5. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

  6. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  7. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  8. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  9. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

随机推荐

  1. @NotNull和@NotEmpty和@NotBlank区别

    1.@NotNull:不能为null,但可以为empty (""," "," ") 2.@NotEmpty:不能为null,而且长度必须大于 ...

  2. C/C++ 语言中的表达式求值(原文作者:裘宗燕)

    经常可以在一些讨论组里看到下面的提问:“谁知道下面C语句给n赋什么值?”m = 1; n = m+++m++;最近有位不相识的朋友发email给我,问为什么在某个C++系统里,下面表达式打印出两个4, ...

  3. 精简CSS

    1.简化你的注释 2.简化颜色代码 3.使用单行属性代替多行属性 4.值为0时可省略单位 5.同时设置多个元素的属性用分组形式 6.删除空白和换行 7.设定过期时间

  4. ZZNU 1995: cots' times

    题目描述 XX年XX月XX日小cot学会了回文数,eg:121. 小cot上课容易走神, 经常看看这个,瞧瞧那个.在小cot某一次走神的过程中他发现电子表上的时间也有回文数... 当然,并不是每次走神 ...

  5. hdu1013

    #include<stdio.h> #include<string.h> int main() { char num[1000]; int len,sum,i; while(s ...

  6. json格式数据 ,将数据库中查询的结果转换为json(方式2)

    controller: /*** * 返回所有版本的信息,json的形式返回到前台 * @return */ @RequestMapping(value="/getAllVersion&qu ...

  7. luci-bwc(记录流量功能)

    使用方法如下: root@openwrt:/# luci-bwcUsage:        luci-bwc [-t timeout] -i ifname             // ifname为 ...

  8. digitalocean纽约机房最先开通IPv6

    DigitalOcean是一家位于美国的云主机服务商,总部位于纽约,成立于2012年.DigitalOcean的服务器全部采用KVM架构,具体高性能处理能力,并且配备SSD固态硬盘,速度优异.每台设备 ...

  9. Chapter 15_0 模块和包

    通常,Lua不会设置规则,相反会提供很多强有力的机制来使开发者有能力实现出最适应的规则. 然而,这种方法对于模块就不行了.模块系统的一个主要目标是允许以不同的形式来共享代码. 但若没有一项公共的规则就 ...

  10. Jenkins - 持续集成环境搭建【转】

    1. Jenkins 概述 Jenkins是一个开源的持续集成工具.持续集成主要功能是进行自动化的构建.自动化构建包括自动编译.发布和测试,从而尽快地发现集成错误,让团队能够更快的开发内聚的软件. 2 ...