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,

题目意思为:给定一个链表和一个数,把这个链表中比这个数小的数全放在比这个数大的数前面,而且两边要保持原来的顺序。

思路有两种。

一是先创建两个新的链表,一个全是比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的更多相关文章

  1. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  2. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  3. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  4. [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间

    Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...

  5. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  6. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  7. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. ubuntu下配置安装conky

    由于默认的conky配置不好看,于是下载了一些配置,网上一抓一大把. 首先  sudo apt-get install conky-all 然后下载想要的配置文件,下载下来的是压缩文件,解压就行了,解 ...

  2. 《how to design programs》第10章表的进一步处理

    返回表的函数: 下面是一个求工资的函数: ;; wage : number  ->  number ;; to compute the total wage (at $12 per hour) ...

  3. jQuery中append html后绑定事件不起作用

    事件一定要紧跟append之后, 否则append元素点击不起作用 $(function(){$('div').append('<ul><li id="appli" ...

  4. 瑞柏匡丞:用全局观开发App

    不管是互联网金融还是生活服务,移动端APP开发都是各大商家关注的重点.众多研发机构的涌入,使得APP的市场环境已经再无技术壁垒可言.在APP市场中,神州锐达的研发水准和客户认同度,一直受到业内人士的称 ...

  5. Wikioi 1169 传纸条

    这道题是我人生第一道双线动规题,因此我觉得还是很有必要记录下来. 刚接触到这道题的时候我第一反应是单线的动规,可是下一秒我就觉得这样做可能会有问题,因为从左上角(以下简称A)到右下角(以下简称B)通过 ...

  6. js点击事件防止用户重复点击执行

    点击事件里给button标签加一个自定义属性,存上次点击时间 追问: 求详细代码,JS 真心的没怎么做过 追答:   <input type="button" id=&quo ...

  7. Spring(三)——AOP

    AOP全名为Aspect-Oriented Programming,意思是面向横切面编程,前边我们有过介绍   面向横切面编程AOP的理解 ,我们通过这种编程思想很容易的扩展我们的应用程序. 一,如何 ...

  8. javascript 命名空间,学习

    一. (function(){ var _NS=function(){ } _NS.prototype.alert=function(){ console.log('test'); } window. ...

  9. C#比较两个时间大小

    DateTime t1 = Convert.ToDateTime("2012-12-31 23:59:00");            DateTime t2 = Convert. ...

  10. json 去空值与缩进

    var jSetting = new Newtonsoft.Json.JsonSerializerSettings(); //忽略值为null的 jSetting.NullValueHandling ...