ParttiTion List 划分链表
来源:互联网 发布:mac flash发热 编辑:程序博客网 时间:2024/05/21 17:17
ParttiTion List 划分链表
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
.
这道题要求我们划分链表,把所有小于给定值的节点都移到前面,大于该值的节点顺序不变,相当于一个局部排序的问题。
package leetcode;
/*
* 要求把小于x的元素按顺序放到链表前面。我们仍然是使用链表最常用的双指针大法,一个指向当前小于x的最后一个元素,
* 一个进行往前扫描。如果元素大于x,那么继续前进,否则,要把元素移到前面,并更新第一个指针。
* */
public class PartionList {
public ListNode partion (ListNode head ,int x){
if(head==null){
return null;
}
ListNode helper=new ListNode(0);
helper.next=head; //使用helper便于直接返回修改后的链表;
ListNode walker=helper;
ListNode runner=helper;
while(runner.next!=null){
if(runner.next.val
- ParttiTion List 划分链表
- Partition List 链表划分区间@LeetCode
- LeetCode(Partition List) 划分链表
- LintCode Partition List 链表划分
- Leetcode 86. Partition List(链表划分)
- 【LeetCode】Partition List 链表划分
- LeetCode2.2.3 @ Partition List 链表划分 D3F3
- LeetCode OJ 之 Partition List (划分链表)
- leetcode 86. Partition List-链表划分|双指针
- Leetcode 86. Partition List 链表划分 解题报告
- 96.Partition List-链表划分(容易题)
- LeetCode 86. Partition List 链表的划分 Python Solution
- 链表划分
- 链表划分
- 题目:链表划分
- LintCode-链表划分
- 链表划分
- 链表划分
- 2017_11_22 学习Json数据与Java对象互转的两种方式(二)
- ComposeMessageFragment源码分析
- 用java写一个简单的随机生成井字棋棋子的程序
- scikit-learn 中文文档-随机梯度下降-监督学习|ApacheCN
- 【LeetCode】389. Find the Difference
- ParttiTion List 划分链表
- 动态绑定按钮点击事件
- XListView
- 如何批量重命名照片,但是去掉那个烦人的括号
- 第一个c语言程序
- Ruby vs Java -- "== 与 equal"
- CUDA学习资源
- OCR 中文汉字识别,可用于文档识别,身份证识别,名片识别,采用字库+卷积神经网络
- GreenDao存集合类型数据