题目

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的前面,并且不改变链表之间node原始的相对位置。每次看这道题我老是绕晕,纠结为什么4在3的前面。。其实还是得理解题意,4->3->5都是大于等3的数,而且这保持了他们原来的相对位置 。

所以,这道题是不需要任何排序操作的,题解方法很巧妙。

new两个新链表,一个用来创建所有大于等于x的链表,一个用来创建所有小于x的链表。

遍历整个链表时,当当前node的val小于x时,接在小链表上,反之,接在大链表上。这样就保证了相对顺序没有改变,而仅仅对链表做了与x的比较判断。

最后,把小链表接在大链表上,别忘了把大链表的结尾赋成null。

代码如下:

 1     public ListNode partition(ListNode head, int x) {
 2         if(head==null||head.next==null)
 3             return head;
 4         
 5         ListNode small = new ListNode(-1);
 6         ListNode newsmallhead = small;
 7         ListNode big = new ListNode(-1);
 8         ListNode newbighead = big;
 9         
         while(head!=null){
             if(head.val<x){
                 small.next = head;
                 small = small.next;
             }else{
                 big.next = head;
                 big = big.next;
             }
             head = head.next;
         }
         big.next = null;
         
         small.next = newbighead.next;
         
         return newsmallhead.next;
     }

Partition List leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. LeetCode算法题-Array Partition I(Java实现)

    这是悦乐书的第262次更新,第275篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第129题(顺位题号是561).给定一个2n个整数的数组,你的任务是将这些整数分组为n对 ...

  3. Partition List -- LeetCode

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

  4. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  5. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  6. kafka topic消息分配partition规则(Java源码)

    我们知道Kafka 的消息通过topic进行分类.topic可以被分为若干个partition来存储消息.消息以追加的方式写入partition,然后以先入先出的顺序读取. 下面是topic和part ...

  7. 561. Array Partition I - LeetCode

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

  8. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

随机推荐

  1. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus

    背景 对于Android系统来说,消息传递是最基本的组件,每一个App内的不同页面,不同组件都在进行消息传递.消息传递既可以用于Android四大组件之间的通信,也可用于异步线程和主线程之间的通信.对 ...

  3. scrapy运行机制

    Scrapy主要包括了以下组件: 引擎(Scrapy)用来处理整个系统的数据流, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回 ...

  4. BZOJ.2428.[HAOI2006]均分数据(随机化贪心/模拟退火)

    题目链接 模拟退火: 模拟退火!每次随机一个位置加给sum[]最小的组. 参数真特么玄学啊..气的不想调了(其实就是想刷刷最优解) 如果用DP去算好像更准.. //832kb 428ms #inclu ...

  5. 随笔idea-辗转落户cnblogs

    辗转了几个blog,也用了自己域名2年,感觉忙起来,可能没有那么多时间去维护自己的域: 其他地方的blog也不在一块,思虑许久后,来到cnblogs:

  6. C++ 刷题常用函数总结

    平时常用C++刷一些算法题,C++内置了许多好用的工具函数,但时间一长总是容易忘记,这里简单做一下总结,方便复习! <stdlib.h> atoi(const char* str) 将一串 ...

  7. poj 3041 Asteroids 最小点覆盖/最大匹配

    Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16242 Accepted: 8833 Descriptio ...

  8. 探秘GO语言《比较C#与GO的性能》

    这段时间也来学学GO语言,听说它的性能相当的棒棒,我就拿C#来和它做比对一下. 这里只是单纯了做了for循环的比对,看看谁的循环快 C# 代码: static void Main(string[] a ...

  9. 全栈project师体能备战--知识面(1--10)

    javascript 单例设计模式:         单例模式确保某个类仅仅有一个势力,并且自行实例化并向整个系统提供这个实例.如:cocos2dx中的导演类.[样例]我有6哥美丽的老婆,他们的老公都 ...

  10. Tasker App Factory

    http://tasker.dinglisch.net/userguide/en/appcreation.html App Creation Introduction Hello World Exam ...