【Leetcode】Partition List (Swap)
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)的更多相关文章
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- 【leetcode】Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- 【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 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
随机推荐
- Linux C语言写的超级简单port扫描器
这个本来曾经也写过的,今天无聊复习下 再写一遍.简单的一塌糊涂,写的不咋地大家见谅哦!有空再加强 嘿嘿! #include <stdio.h> #include <stdlib.h& ...
- 远程centos改动yum源
yum -y install unzip发现运行不了,说是找不到unzip的包,搜索发现时由于yum源的问题,那我就改动yum吧, 在网上找到的方法是这么说的: 1. cd /etc/yum.repo ...
- IOS开发笔记 - 基于wsdl2objc调用webservice
为了方便在ios下调用webserivce,找来了wsdl2objc这样一个开源的框架来解析webservice方便在ios下引用. 下面做个小例子. 1.首先是用Asp.net搭建一个测试的webs ...
- 一个使用Java jdk8中Nashorn(Java javascript引擎)设计的Web开发框架
地址:https://github.com/iboxdb/hijk 採用给框架开发应用,简单直接.开发效率高 下载后 set PATH to /JAVA 8_HOME/bin jjs build.js ...
- App如何选择移动广告平台的开发者3 - 选择标准广告平台
App开发公司.通常他们不能走品牌.要挑品牌的能力,我们将面临两大问题:业务团队.广告填充率.一系列的问题,以现金周期. 无线商务本才刚刚开始,大多数都是没有商业经验.产品.设计.运营.销售的人才都不 ...
- xamarin之 安装工具介绍
原文:xamarin之 安装工具介绍 思考: 1, 一定要按照顺序安装吗? 先装JDK,再装Android SDK 原因:Android SDK采用了Java语言 先装Android SDK,再装A ...
- android AIDL RPC 机制
AIDL 这是接口文件的叙述性说明,为了实现android 上述平台RPC ,aapt 在编译时自己主动按照该规则IPC 的接口和对象,作为一个用户只需要 实现在服务侧的界面 2 在clientbin ...
- 领域驱动设计(DDD)部分核心概念的个人理解(转)
领域驱动设计(DDD)是一种基于模型驱动的软件设计方式.它以领域为核心,分析领域中的问题,通过建立一个领域模型来有效的解决领域中的核心的复杂问题.Eric Ivans为领域驱动设计提出了大量的最佳实践 ...
- Android呼叫开发系列WebService
我在学习Android第一个问题是在发展进程中遇到Androidclient究竟是怎么用server与数据库交互它?问题是,我有初步接触Android这困扰了我一个非常大的问题.天直到几年前,我突然想 ...
- 【转】QT样式表 (QStyleSheet)
作者:刘旭晖 Raymond 转载请注明出处Email:colorant@163.comBLOG:http://blog.csdn.net/colorant/ 除了子类化Style类,使用QT样式表( ...