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 ...
随机推荐
- .net利用本地播放器播放视频文件代码
前台点击按钮,执行js事件,跳转到后台代码: function funShowVideo(index) { var iTop = (window.screen.availHeig ...
- ajax 异步调用把返回值赋给一个全局变量的用法,最主要的就是把async属性改为 false,
<script> $(document).ready(function () { <% string dqsj = System.DateTime.Now.ToString(&quo ...
- gulp 制作雪碧图
雪碧图:sprite 是把多张图片拼到一张图中,提升性能的一种做法.把合并的图片一次性加载到内存中,需要时只渲染一部分. 我们选择gulp.spritesmith插件. 使用gulp时首先要在指定的任 ...
- API中FileReader 接口事件
FileReader 接口包含了一套完整的事件模型,用于捕获读取文件时的状态onabort 中断onerror 出错onloadstart 开始onprogress ...
- JavaWeb学习记录(二十三)——文件上传与下载
一.导入jar包
- 解决dede搜索页面只能显示10条信息解决方案
解决dede搜索页面只能显示10条信息解决方案,感觉显示的信息太少,这时就要想办法去解决一下.看看有什么好办法来解决一下这个问题. dede搜索页模板中,默认只能显示10条记录. 打开dede搜索页模 ...
- 【P1326】超级教主
DP优化 原题: LHX教主很能跳,因为Orz他的人太多了.教主跳需要消耗能量,每跳1米就会消耗1点能量,如果教主有很多能量就能跳很高.教主为了收集能量,来到了一个神秘的地方,这个地方凡人是进不来的. ...
- HTML DOM参考手册
HTML DOM是HTML Document Object Model(文档对象模型)的缩写,HTML DOM则是专门适用与HTML/XHTML的文档对象模型.熟悉软件开发的人员可以将HTML DOM ...
- DuiLib——第二篇UIBase
---------------------------------------------------------------------------------- 分析约定: private o-- ...
- 在CentOS里使用MySQL Connector/C++
操作系统版本:CentOS6 64位 1,安装boost库.因为MySQL Connector/C++使用了boost库,所以必须先安装boost库,我们才能使用MySQL Connector/C++ ...