86. 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.
==========
题目:
有一个数字链表和单独的值x,重新划分链表,
将链表中的所有小于x的节点放到 大于等于x的节点前面,
小于x的节点的相对顺序不变,大于等于x的节点的相对顺序不变.
--------------
思路:
和奇数偶数排序一样类似,
遍历链表的时候,将节点分组,
难点在于怎么判断开始?怎么判断链表结束?
定义两个假的头节点dummy1,dummy2,利用两个p1和p2分别指向前两个节点
外加一个遍历list的指针curr,
最后将dummy1链表的尾指向dummy2链表的头节点(这里dummy1和dummy2都是节点,不是指针).
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(head==nullptr || head->next==nullptr) return head;
ListNode *head1,*head2;
ListNode *p1,*p2,*curr;
ListNode dummy1(-);
ListNode dummy2(-);
curr = head;
p1 = head1 = &dummy1;
p2 = head2 = &dummy2; while(curr){
if(curr->val < x){
p1->next = curr;
p1 = p1->next;
//p1->next = nullptr;
}else{
p2->next = curr;
p2 = p2->next;
//p2->next = nullptr;
}
curr = curr->next;
}
p1->next = nullptr;
p2->next = nullptr;
//showList(head1->next);
//showList(head2->next); p1->next = head2->next;
return head1->next;
}
};
86. Partition List的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- leetcode 86. Partition List
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 解题思路
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode OJ 86. Partition List
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode 86. Partition List 划分链表 C++
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划分链表
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
Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...
随机推荐
- call & apply
对于apply和call两者在作用上是相同的:这两个方法通常被用来类的继承和回调函数.但两者在参数上有区别的.call函数和apply方法的第一个参数都是要传入给当前对象的对象,及函数内部的this. ...
- html部分---样式表,选择器;
<1.内联样式,优点:控制精确,缺点:代码重用性差,页面代码乱.> <div style="background-color:#0F0"></div& ...
- spark新能优化之数据本地化
数据本地化的背景: 数据本地化对于Spark Job性能有着巨大的影响.如果数据以及要计算它的代码是在一起的,那么性能当然会非常高.但是,如果数据和计算它的代码是分开的,那么其中之一必须到另外一方的机 ...
- hdu1025 dp(最长上升子序列LIS)
题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...
- install gcc under suse
SUSE 11中安装GCC开发环境 SUSE11中安装GCC开发环境 安装包下载网站:http://213.174.32.130/sles/distribution/11.0-SP1/repo/dis ...
- strlen() 函数
strlen() 函数通常用来计算字符串的长度,但是今天突然发现个奇怪的现象. 如下所示: #include <stdio.h> #include <stdlib.h> #in ...
- checking
https://en.wikipedia.org/wiki/Java_performance https://en.wikipedia.org/wiki/Java_virtual_machine ht ...
- 02-Java 数组和排序算法
一.Java 数组 1.数组定义:数组是有序数据的集合,数组中的每个元素具有相同的数组名和下标来做唯一标识. 2.数组的分类:一维.二维.三维. 3.数组声明及内存分配: 为数组分配内存空间:如果不分 ...
- cocos2d-x,求世界坐标
老版: http://user.qzone.qq.com/350479720/blog/1384483239 一,求node的世界坐标.因为node的contentSize为0,局部坐标原点与node ...
- Ajax编程中,经常要能动态的改变界面元素的样式
在Ajax编程中,经常要能动态的改变界面元素的样式,可以通过对象的style属性来改变,比如要改变背景色为红色,可以这样写:element.style.backgroundColor=”#ff0000 ...