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. 转:LINQ查询返回DataTable类型

    动态绑定ReportViewer虽然之前实现过,但现在弄起来还是有点晕,主要是过去没有使用Linq,数据的操作经常用到DataTable,可以直接拿来使用,现在用Linq更方便,也懒得再用之前的数据库 ...

  2. VS2008远程调试

    环境:      同一局域网内,主机和虚拟机远程调试   远程计算机:虚拟机搭的WindowsXP/32(局域网中使用桥接,非局域网使用NAT)     本地计算机:Windows XP.Win71. ...

  3. Valid Palindrome 解答

    Question Given a string, determine if it is a palindrome, considering only alphanumeric characters a ...

  4. Impala 1、Impala理论

    1.Impala简介 • Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能. • 基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 ...

  5. Python 自动化脚本学习(三)

    函数 例子 def hello(): print("hello" + "world"); 有参数的函数 def hello(name): print(" ...

  6. android面试题之五

    二十六.什么情况会导致Force Close ?如何避免?能否捕获导致其的异常? 抛出运行时异常时就会导致Force Close,比如空指针.数组越界.类型转换异常等等. 捕获:可以通过logcat查 ...

  7. Linux常见目录作用

    Linux中一切皆文件 文件类型: 一般文件 - 目录文件 d 链接文件 l 块设备   b    (以块为单位进行操作,比如硬盘) 字符设备 c  (以字符为单位进行操作,比如主存) socket  ...

  8. <audio> 标签简介

    定义和用法 <audio> 标签定义声音,比如音乐或其他音频流. 实例 一段简单的 HTML 5 音频: <audio src="someaudio.wav"&g ...

  9. cisco asa5520 IOS故障恢复

    在ASA5520上误删除了IOS 把一台电脑IP 设置成192.168.2.2 255.255.255.0 把IOS的BIN文件复制到这台电脑上,并安装一个CISCO TFTP SERVER V1.1 ...

  10. Asp.net MVC razor语法参考

    Razor语法的快捷参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ 只是copy下来便于查阅! I ...