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 greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
有点用到了倒置链表II的方法,将符合要求的结点放置在指向pre指针的后面。这道题的思路应该是找到第一个大于等于x值的结点,他前一个位置始终定位pre指针,放置比x小的结点。
方法一(C++)
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* dummy=new ListNode(-);
dummy->next=head;
ListNode* pre=dummy,* cur=head;
while(pre->next&&pre->next->val<x)
pre=pre->next;
cur=pre;
while(cur->next){
if(cur->next->val<x){
ListNode* t=cur->next;
cur->next=t->next;
t->next=pre->next;
pre->next=t;
pre=t;
}
else
cur=cur->next;
}
return dummy->next;
}
};
LeetCode 86. Partition List 划分链表 C++的更多相关文章
- [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]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
- 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 ...
- [CareerCup] 2.4 Partition List 划分链表
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...
- [LeetCode] 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 ...
- 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 ...
随机推荐
- javascript获取某个月份的天数
function DayNumOfMonth(Year,Month) //例DayNumOfMonth(2017,05);{ Month--; //月份是0~11.month=4 var ...
- js页面加载完成事件
jquery的一种简写形式: $(function(){ alert("页面加载完成!"); }); 其对应的完整形态为: $(document).ready(function( ...
- MFC中给各个控件填充背景颜色的方法
1.给程序设置大背景色,在OnPaint()函数中添加如下代码: CRect rect; CPaintDC dc(this); GetClientRect(rect); dc.FillSolidRec ...
- cordova本地浮动框提示插件使用:cordova-plugin-x-toast
1. 添加插件:cordova plugin add cordova-plugin-x-toast 2. 调用方法(浮动提示插件,弹出本地浮动提示框): $cordovaToast.show(mess ...
- streamreader 和 streamwriter 以及 string 与 memorystream 使用示例
经常用到,但老记不住,备忘一下 using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); sw.WriteLine(&q ...
- Collections.sort排序
默认是升序,即Collections.sort(list),对list进行升序排列,如果想降序则需要通过compare这些参数来实现了
- python 基础之python的六大标准数据类型
一:Number 数字类型(int float bool complex) 1.整型: (正整数 0 负整数)#(1)二进制用0b表示intvar = 0b1010print(intvar)pr ...
- Asp.Net : Page.RegisterStartupScript及 不执行的原因
RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部 ...
- 【环境部署】centos7安装mysql-5.7.19 group-replication
--mysql高可用官方文档: https://dev.mysql.com/doc/refman/5.7/en/group-replication.html mysql下载地址: https://ww ...
- VirtualApk 插件入门小试
1 官方资料 滴滴开源Android插件方案:VirtualAPK 2 宿主App集成方法 (1)在整个工程的build.gradle中添加依赖 dependencies { classpath 'c ...