【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: ( ...
随机推荐
- Java 二次MD5 32位小写加密算法与php页面加密结果相同
最近做的一个项目需要使用MD5加密算法,需要加密的参数有两个.自己先试了几次,算的结果为php页面的不一样,后来与写php页面的同事沟通后,了解到php页面的算法如下: action = " ...
- c#和UDP SOCKET广播
server: Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp); // ...
- 初识缓存以及ehcache初体验
1.缓存的意义 缓存机制就是将数据库中经常使用的数据取出放入内存中.程序调用时直接从内存中取,丌用每次使用 数据都訪问数据库,这样提高了效率. 2.缓存须要关注的问题 1) 缓存的更新 缓存中的数 ...
- J2SE基础:1.类和对象基础
什么是对象 在Java语言,全部的人,事物或者模块都是一个对象. 同样的对象具有一些同样的特性. 狗,猫,蛇3个对象(动物的对象) 苹果,梨,桔子3个对象(水果的对象) 什么是类 能够将现实生活中的对 ...
- Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)
Qt/C++开发人员眼中的Obj-C 对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...
- 白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》串行4(场景变化)
作为一个真正的利用价格值应用,首先,你应该至少有两页,通过切换页面来实现很多其他互动.比如手机人人网,首先,打开后进入登录页面,将有登录后,新的东西.然后拉左侧面板.你可以看到相册.私人信息.像其他应 ...
- 使用Ratpack和Spring Boot打造高性能的JVM微服务应用
使用Ratpack和Spring Boot打造高性能的JVM微服务应用 这是我为InfoQ翻译的文章,原文地址:Build High Performance JVM Microservices wit ...
- SharePoint 2010 新列表模板列表
SharePoint 2010 新列表模板列表 项目描述叙事 发展环境创造了良好的名单为模板.然后使用列表模板将其复制到生产环境. 脚步 1. 打开"列表设置",找到"将 ...
- 线性表实现简单vector
实现一个简单的vector Vector基于数组实现,可以复制并且其占用的内存可以自动回收(通过析构函数),可以调整Vector的大小,以及容量(容量的改变是通过为基本数组分配一个新的内存块,然后复制 ...
- 【v2.x OGE课程 14】 控制使用
在这里,精灵.动画精灵.button天才.经常使用的文本的使用 一个.相关精灵 1.加入精灵 //创建精灵 Sprite bar_up = new Sprite(400, 0, RegionRes.g ...