Leetcode解题-链表(2.2.3)PartitionList
题目:2.2.3 Partition List
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.
代码实现
我们可以分两步解决:
1)遍历链表时,将其一分为二:第一次添加结点,即创建分区时,记住两个分区的header(par1和par2)。之后,用par1p和par2p指向分区的尾部,便于每次迭代向两个分区添加结点。
2) 链接两个分区:将分区2链接到分区1的尾部,并将分区2最后一个结点的next赋值为NULL(非常重要,后面会解释!)。
野指针引发的血案
指针par1,par2都没有初始化为NULL,而后面的if却用par1或par2是否等于NULL作为初始化par1h和par2h的条件,另外最终partition1和partition2拼接后,未将最后一个结点的改为NULL,这两个初始化问题都将导致意想不到的运行结果(产生了循环链表,打印时造成死循环)。)变量除非后面立刻赋值,否则一定要初始化;2)处理链表时,尤其是对一个链表动“大手术”时,一定要注意是不是有next值没有改,导致循环链表。
链表的典型处理方法
前面的实现中,因为初始时指针为空,所以第一次使用之前必须初始化,这使代码变得复杂化了。我们可以添加两个dummy header来简化这个问题,与实现链表的insert()和delete()时的技巧相同。Dummy分配在栈上,函数返回时将直接被回收。总结:不仅是实现链表,只要对链表做大改动,特别是要动头结点时,使用dummy header能让代码变得简洁并且不易出错。
附:二级指针实现
二级指针同样非常简洁,而且也不用在栈上分配两个dummy对象,也许速度上能更快一些。
Leetcode解题-链表(2.2.3)PartitionList的更多相关文章
- Leetcode解题-链表(2.2.0)基础类
1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø 规定好每个子Solution都要实现纯虚函数test做测试: Ø 提供了List ...
- Leetcode解题-链表(2.2.6)RotateList
1 题目:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Fo ...
- Leetcode解题-链表(2.2.2)ReverseLinkedList
题目:2.2.2 Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in on ...
- Leetcode解题-链表(2.2.1)AddTwoNumbers
1 题目:2.2.1 Add Two Numbers You are given two linked lists representing two non-negative numbers. The ...
- Leetcode解题思想总结篇:双指针
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
随机推荐
- 数据库性能优化之SQL语句优化(上)
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统的 ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] Add One Row to Tree 二叉树中增加一行
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- [AtCoder arc090E]Avoiding Collision
Description 题库链接 给出一张 \(N\) 个节点, \(M\) 条边的无向图,给出起点 \(S\) 和终点 \(T\) .询问两个人分别从 \(S\) 和 \(T\) 出发,走最短路不相 ...
- [UOJ]#33. 【UR #2】树上GCD
题目大意:给定一棵有根树,边长均为1,对于每一个i,求树上有多少个点对,他们到lca距离的gcd是i.(n<=200,000) 做法:先容斥,求出gcd是i的倍数的点对,考虑长链剖分后从小到大合 ...
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- 【The Time Traveller's Wife】
After reading The Time Traveller's Wife: It's a tragedy,I think.But it's mixed with hope.Henry ...
- bzoj3294[Cqoi2011]放棋子 dp+组合+容斥
3294: [Cqoi2011]放棋子 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 294[Submit][Status] ...
- cocos2d-x-3.x 学习总结(一)
这周学习了<cocos2d-x 3.x 游戏开发之旅>的第三章,做如下总结: 1.关于创建标签对象 书中是 Label* label = Label::create(); 可是总是提示出错 ...