【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: ( ...
随机推荐
- 使用异步HTTP提升客户端性能(HttpAsyncClient)
使用异步HTTP提升客户端性能(HttpAsyncClient) 大家都知道,应用层的网络模型有同步.异步之分. 同步,意为着线程阻塞,只有等本次请求全部都完成了,才能进行下一次请求. 异步,好处是不 ...
- 在mysql数据库中关于日期时间字段的处理
在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...
- J2EE的13个规范之JDBC
假设让你接触一样新的东西.你可能感觉无所适从,可是假设本来就是旧事物的话,你学习起来还难吗? 一.ODBC,我们的老朋友 ODBC(Open Database Connectivity)是微软公司与数 ...
- 学习笔记 Android.mk 搜索自己主动
最近一直Android.mk这是什么一个令人沮丧的夜晚,点击此处记录. ios你担心更多.不管那么多.xcode自己解决. 文本工具:MACVIM(文本编辑工具 很有用 你可以清楚地分辨tab 和Sp ...
- android EditText的美化
今天要做一个页面,有EditText,于是就搞起了它的美化. EditText的美化,我的第一反应是,在EditText的左边设置显示一张图片,这样会比較好看. 设置左边显示图片的属性为:androi ...
- Qt中截图功能的实现
提要 需求:载入一张图片并显示,能够放大缩小,能够截取图片的某个矩形并保存. 原以为蛮简单的一个功能,事实上还是有点小复杂. 最简单Qt图片浏览器能够參考Qt自带的Demo:Image Viewer ...
- Android实战技术:IPC方式简介教程
非实时,通知性的方式 第一种方式就是Intent,Intent可以非常方便的通讯,但是它是非实时的,无法进行实时的像函数调用那样的实时的通讯. 实时的函数调用 但是IPC的根本目的还是为了实现函数的调 ...
- BZOJ 1449 JSOI2009 球队收益 费用流
题目大意:给定nn支球队.第ii支球队已经赢了winiwin_i场.输了loseilose_i场,接下来还有mm场比赛.每一个球队终于的收益为Ci∗x2i+Di∗y2iC_i*x_i^2+D_i*y_ ...
- oracle 关于日期格式转换与使用
在oracle中我们经常会和日期打交道,在做报表的时候经常会用日报,周报,月报之类的条件进行分组: 我写了些例子来启发下大脑 select Sysdate from dual select to_ch ...
- js调用百度地图接口
原文:js调用百度地图接口 这是前几天公司做的新项目,上面需要用到地图的数据.第一次做这类型的东西没啥思路,咱们经理说,这东西简单,截个图存文件夹里调整好尺寸,数据库里存上图片的地址动态调用就行了.心 ...