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. Calendar中add函数和roll函数的用法及区别

    Calendar中add()和roll()函数的用法一.取某个时间点后的整点时刻.例如1984年7月7日15:23:05后的整点时刻即为1984-07-07 16:00:00.实现如下:Calenda ...

  2. DDMS files not found:hprof-conv.exe的解决办法

    或者是you must restart adb and eclipse这类错误 原因:一般是豌豆荚之类的软件影响的,所以,以后要慎用了. 解决方案:先找一下在sdk\tools目录下是否有hprof- ...

  3. C#中给线程传值并启动

    方法1: ParameterizedThreadStart 委托+Thread.Start 方法 (Object)         private void btnLogin_Click(object ...

  4. javascript代码混淆原理

    https://www.google.com/search?biw=1440&bih=729&q=javascript%E4%BB%A3%E7%A0%81%E6%B7%B7%E6%B7 ...

  5. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.0

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  6. UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>

    L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/ ...

  7. UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>

    J - Islands Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  8. Unity 读取Excel

    游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入excel中来读取 第一种解决方案: xlsx –> csv –> 改变成UTF-8 或者Unicode编码 –& ...

  9. struts2的<constant/>标签使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  10. 对Textbox的值转换为带千位符和小数的Decimal字符串

    以下Function可以用于textbox的KeyUp事件: 2014-06-06 发现旧版IE不支持selectionStart还有字符串的"[]"索引获取值, 已经修复这个bu ...