【LeetCode练习题】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,
Given1->4->3->2->5->2
and x = 3,
题目意思为:给定一个链表和一个数,把这个链表中比这个数小的数全放在比这个数大的数前面,而且两边要保持原来的顺序。
思路有两种。
一是先创建两个新的链表,一个全是比x小的数,一个全是比x大的数。然后合并这两个链表。
第一种方法比较容易理解,具体可以参考:http://blog.csdn.net/havenoidea/article/details/12758079
下面给出第二种方法的代码。
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; //用cur和pre指向大于等于x的第一个节点和上一个节点,为了考虑第一个节点就大于x的情况,增加一个dummy的头结点便于处理方便。
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head == NULL){
return head;
}
ListNode *dummy = new ListNode();
dummy->next = head;
head = dummy;
bool hasGreater = false;
//找到第一个大于等于x的节点,用cur指向它
ListNode *pre = head, *cur = head->next;
while(cur!=NULL){
if(cur->val < x){
pre = pre->next;
cur = cur->next;
}
else{
hasGreater = true;
break;
}
}
//将cur节点之后所有的小于x的节点都挪到pre的后面,cur的前面。
if(hasGreater){
ListNode *pre2 = head, *cur2 = head->next;
bool isBehind = false;
while(cur2!=NULL){
if(cur2->val == cur->val){
isBehind = true;
}
if(isBehind && cur2->val < x){
pre2->next = cur2->next;
pre->next = cur2;
cur2->next = cur;
cur2 = pre2->next;
pre = pre->next;
}
else{
pre2 = pre2->next;
cur2 = cur2->next;
}
}
}
//去掉dummy节点。
head = head->next;
delete dummy;
return head;
}
};
【LeetCode练习题】Partition List的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- [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 ...
随机推荐
- Remove Element 解答
Question Given an array and a value, remove all instances of that value in place and return the new ...
- android 分享到新浪微博
分享到新浪微博,折腾了大半个月,现在终于弄出来了,心里的那个爽呀,太痛快了,哈哈!! 废话少说,首先是认证, 1.进入新浪微博提供的开放平台注册新浪账号. 2.点击’我是开发者‘,创建一个应用,得到C ...
- MP3/WAV 播放
一.编译libmad 1.先下载压缩包到本地,并解压 tar -xvzf libmad-0.15.1b.tar.gz -C ./ 2.进入源代码文件夹并配置 编写一个配置文件,便于< ...
- Animate.css 教程
animate.css 是一个有趣,酷炫的,跨浏览器的动画库,你可以将它用于你的项目中.不管是主页,滑动切换,又或者是其它方面,你都可以通过它来制作出惊人的效果. 基本用法 引入CSS文件 这个对你来 ...
- GTID复制报错处理:Last_Error: Error 'Can't drop database 'test'; database doesn't exist' on query
创建GTID主从连接: mysql, master_user; 报错显示: Slave_IO_Running: Yes Slave_SQL_Running: No Last_Error: Error ...
- with ffmpeg to encode video for live streaming and for recording to files for on-demand playback
We've been doing some experimentation with ffmpeg to encode video for live streaming and for recordi ...
- webapi单元测试时出现的ConfigurationManager.ConnectionStrings为空错误
这个是读取配置文件没读到的问题,解决方法很简单,把webapi的配置文件复制到单元测试项目中,并把名字改为App.config即可. 同时 ,推荐使用Unit Test Genertor来做测试,这个 ...
- 常用SQL Server分页方式
假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YE ...
- JS转换Decimal带千分号的字符串显示
var numberChars = "0123456789"; /* Convert to decimal string */ function toDecimalString(v ...
- Sublime+Emmet
Sublime使用Package Control安装Emmet插件: 按Ctrl+Shift+P命令板 输入install然后选择install Package,然后输入emmet找到 Emmet C ...