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的前面。并且相对顺序不能变

这个时候我们採用双指针法

runner1用于寻找最后一个比x小得元素,这里作为插入点(切割点)

runner2用于寻找应该插到插入点之后的元素

所以这里会出现三种情况

情况1: runner2的元素小于x。这个时候假设runner1和runner2指向同一个元素。说明还没有找到切割点,所以两个指针继续往前走即可了。

情况2: runner2的元素小于x。这个时候假设runner1和runner2指向不同的元素。说明runner1已经走到了切割点前,而runner2.next就是应该被插到切割点前。把runner2.next插入到切割点前就ok

情况3: runner2的元素大于x, 这个时候找到了切割点,仅仅移动runner2就能够了。直到runner2.next小于切割点。

然后依照情况2来操作就能够了

情况1和情况2:

			if (runner2.next.val < x) {
if (runner1 == runner2) {
runner1 = runner1.next;
runner2 = runner2.next;
} else {
ListNode insert = runner2.next;
ListNode next = insert.next;
insert.next = runner1.next;
runner1.next = insert;
runner2.next = next;
runner1 = runner1.next;
}
}

情况3:

			else
runner2 = runner2.next;

完整代码例如以下

	public ListNode partition(ListNode head, int x) {
if (head == null)
return head; ListNode helper = new ListNode(0);
helper.next = head;
ListNode runner1 = helper;
ListNode runner2 = helper; while (runner2 != null && runner2.next != null) {
if (runner2.next.val < x) {
if (runner1 == runner2) {
runner1 = runner1.next;
runner2 = runner2.next;
} else {
ListNode insert = runner2.next;
ListNode next = insert.next;
insert.next = runner1.next;
runner1.next = insert;
runner2.next = next;
runner1 = runner1.next;
}
} else
runner2 = runner2.next;
}
return helper.next;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

【Leetcode】Partition List (Swap)的更多相关文章

  1. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  2. 【leetcode】Partition List

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

  3. 【leetcode】Partition List(middle)

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

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

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

  5. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  6. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  7. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  8. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  9. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

随机推荐

  1. GEF的MVC体系结构

    摘要: 本文首先介绍了标准的 MVC 体系构架,同时也介绍了最常见的一类 MVC 模式的变种.之后,文章重点介绍了 MVC 结构在 gef 框架中的体现与应用,以及 gef 是如何综合利用工厂模式.命 ...

  2. 简单工厂 VS 工厂方法 VS 抽象工厂

    说到设计模式.自然少不了简单工厂模式.工厂方法和抽象工厂这三姐妹. 它们之间可谓是各有所长,术业专攻啊!这篇博客来简单的梳理一下三者之间的关系. 那么工厂又是什么意思呢?结合三者的特点,我觉得能够这样 ...

  3. iOS使用CoreImage处理图像40中可用的滤镜名称

    NSString* localPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"] ...

  4. JQuery日记_5.13 Sizzle选择器(六)选择器的效率

        当选择表达式不符合高速匹配(id,tag,class)和原生QSA不可用或返回错误时,将调用select(selector, context, results, seed)方法,此方法迭代DO ...

  5. 经常使用的js小方法

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% Strin ...

  6. Cocostudio学习笔记(4) LoadingBar+ TextField

    这同时录制两个控件的使用方法:LoadingBar和 TextField. -------------------------------------------------------------- ...

  7. POJ 2250 Compromise (UVA 531)

    LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...

  8. DOMContentLoaded和window.onload

    相信写js的.都知道window.onload吧,可是并非每一个人都知道DOMContentLoaded,事实上即使你不知道.非常有可能你也常常使用了这个东西. 普通情况下,DOMContentLoa ...

  9. [原创] linux 下上传 datapoint数据到yeelink 【golang版本】

    package main /* Create by sndnvaps<sndnvaps@gmail.com> * date : 2015-04-05 * upload datapoint ...

  10. android Animation动画的xml使用

    在Android应用程序,使用动画效果,能带给用户更好的感觉,做动画能够通过XML或Android代码来实现. Animation动画效果的实现能够通过两种方式进行实现,一种是tweened anim ...