lc面试准备:Partition List
1 题目
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
.
接口
ListNode partition(ListNode head, int x)
2 思路
- new两个新链表,一个用来创建所有大于等于x的链表,一个用来创建所有小于x的链表。
- 遍历整个链表时,当当前node的val小于x时,接在小链表上,反之,接在大链表上。这样就保证了相对顺序没有改变,而仅仅对链表做了与x的比较判断。
- 最后,把小链表接在大链表。
复杂度
3 代码
// 思路2:dummy1 小于x的linkedlist; dummy2 大于等于x的linkedlist.
public ListNode partition(ListNode head, int x) {
ListNode dummy1 = new ListNode(-1);
ListNode dummy2 = new ListNode(-2);
ListNode t1 = dummy1, t2 = dummy2, pCur = head;
while (pCur != null) {
ListNode pTmp = pCur.next;
if (pCur.val < x) {
t1.next = pCur;
t1 = t1.next;
} else {
t2.next = pCur;
t2 = t2.next;
t2.next = null;
}
pCur = pTmp;
}
t1.next = dummy2.next;
return dummy1.next;
}
4 总结
- Two Pointer的思想
- 这是普通的链表操作,应该熟练掌握。
- 思路1实现细节要多些,用一个头结点、2个指针可以完成。
- 思路2实现简单明了。
5 参考
lc面试准备:Partition List的更多相关文章
- lc面试准备:Remove Duplicates from Sorted List
1 题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- lc面试准备:Implement Stack using Queues
1 题目 Implement the following operations of a stack using queues. push(x) -- Push element x onto stac ...
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- lc面试准备:Power of Two
1 题目 Given an integer, write a function to determine if it is a power of two. 接口 boolean isPowerOfTw ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- lc面试准备:Reverse Bits
1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
随机推荐
- oracle修改登录认证方式
通过配置sqlnet.ora文件,我们可以修改oracle登录认证方式. SQLNET.AUTHENTICATION_SERVICES=(NTS);基于操作系统的认证 SQLNET.AUTHENTIC ...
- Chapter 3. Installing Gradle 安装gradle
3.1. Prerequisites Gradle requires a Java JDK or JRE to be installed, version 6 or higher (to check, ...
- Windows2012中Python2.7.11+Python3.4.4+Pycharm
下载软件包 Python2.7.11: https://www.python.org/ftp/python/2.7.11/python-2.7.11.amd64.msi Python3.4.4: ...
- Genymotion无法启动Virtual Box
Genymotion是非常快速的Android模拟器.这两天搞了一下Android Studio,想用Genymotion跑起一下,但死活都启动不了.很奇怪,明明几个月前还顺利启动的. Genymot ...
- jquery插件下载地址
以下是本人收集的jquery插件下载地址: .............版本自行选择. jquery官网:http://jquery.com/ jquery.validate.js 官网下载地址:htt ...
- js给当前日期加一天
<script type="text/javascript"> function addDay(datetime, days) { var old_time = new ...
- 懒人神器之T4模板
最近遇到一个比较令人烦躁的问题,特别是对于我等懒癌末期者.实在难以忍受!具体问题是这样,这个项目是一个新的项目.使用EF框架来开发,那么在搭建架构时,当我们新加一个Entity时,就需要在每个层级添加 ...
- oc 获取当前时间
//获取当前日期 NSDate* date = [NSDate date]; NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; ...
- 5 DML语言
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Linq 构造复杂Json 多表group by
一个主表A(a1,a2),子表B(a1,b1,b2) ,想得到的结果是 [{a1,a2,Info [{b1,b2},{b1,b2},...}]] var list= from a in A join ...