Partition List
2015-10-03 12:17
447 查看
题目:
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
return
分析:
意思是比x小的元素按顺序放到x前面,按元素原来的顺序,而不是从小到大排序;
参考代码如下:
http://blog.csdn.net/linhuanmars/article/details/24446871
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->2and x = 3,
return
1->2->2->4->3->5.
分析:
意思是比x小的元素按顺序放到x前面,按元素原来的顺序,而不是从小到大排序;
参考代码如下:
http://blog.csdn.net/linhuanmars/article/details/24446871
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode partition(ListNode head, int x) { if(head == null) return null; ListNode helper = new ListNode(0); helper.next = head; ListNode walker = helper; ListNode runner = helper; while(runner.next!=null) { if(runner.next.val<x) { if(walker!=runner) { ListNode next = runner.next.next; runner.next.next = walker.next; walker.next = runner.next; runner.next = next; } else runner = runner.next; walker = walker.next; } else { runner = runner.next; } } return helper.next; } }
相关文章推荐
- 高可用网站的软件质量保证
- struts2+spring+hibernate框架总结(框架分析+环境搭建+实例源码下载)
- 浅谈HTML5之二:新增的元素和废除的元素
- AndroidUI 引导页面的使用
- ACM起步
- 散列表(hash table)——算法导论(13)
- 浅谈HTML5之一:语法的改变
- JDK源码阅读(三) Collection<T>接口,Iterable<T>接口
- AndroidUI 引导页面的使用
- 下载、编译Android源码简明版
- C#学习日记04---数据类型 之 整数类型
- QT5 学习之路14---对象模型
- POJ 1236 Network of Schools(强连通缩点)
- JavaScript显示和隐藏某个div
- Android开发之基础------------测试相关、sdcard操作、SharedPreferences存取
- HDU 3831 DICS
- java模拟线程死锁
- 判断一个树是不是平衡二叉树
- Ant之build.xml详解
- poj 1948 dp(组成面积最大的三角形)