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 ...
随机推荐
- JAVA乐观锁、悲观锁实现
一.名词解释 1.悲观锁:认为每次对数据库的操作(查询.修改)都是不安全的,因此每次操作都会把这条数据锁掉,直到本次操作完毕释放该锁 2.乐观锁:查询数据的时候总是认为是安全的,不会锁数据:等到更新数 ...
- python中常见的报错,总结一下,以后看。
AttributeError 试图访问一个对象没有的属性,比如,乔峰.nameIOError 输入.输出异常:基本上无法打开文件ImportError 无法引入模块或者是包:基本上是路径问题,或者是名 ...
- WMware Vsphere取消某虚机的漂移
由于一些业务特性,有一些虚机不应该完全受集群DSR控制.下面记录一下如何更改某一虚机的漂移属性. 1.环境 VMware Vsphere web client 6.5 2.点击需要配置虚机所在的集群, ...
- nginx ssl 配置
user www www; worker_processes auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/ng ...
- C#如何调用以管理员身份运行的cmd命令提示符
windows自从vista.win7.win8/8.1以及win10以来,命令行提示符分为两种模式,一种是普通用户模式,一种的管理员模式,很多情况下,我们的程序需要在命令提示符(管理员身份)的状况下 ...
- create-react-app源码解读之为什么不搞个山寨版的create-react-app呢?
最近把 vue-cli@2.x 和 create-react-app 的源码都看了一遍.由于现在官方推荐使用 vue-cli@3.0 ,改动比较大,所以就不写关于 vue-cli 的了(据说是因为 v ...
- Windows7 Autoconfiguration IPv4 Address 导致无法上网
Windows7 Autoconfiguration IPv4 Address 导致无法上 (2010-03-30 16:44:57) 转载▼ 标签: 杂谈 分类: 电脑软件问题 Windows7 A ...
- HttpWebResponse Post 前端控件数据,后台如何接收?
MVC视图页: @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>test</ti ...
- ActiveMq报错Channel was inactive for too (>30000)long
生成环境的activemq 隔一到两周,就报错: 查看 activeme的日志: 2018-12-04 11:59:44,744 | WARN | Transport Connection to: ...
- StringRedisTemplate常用操作
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向r ...