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. kibana 日志查看界面

  2. 外部样式OL LI的定义 影响到了富文本框内的UL LI的定义,使用内部样式对其还原

    <style type="text/css"> #intro { white-space: normal; word-break: break-all; overflo ...

  3. 几个js的linq实现

    几个js的linq实现 linqjs.codeplex.com jslinq.codeplex.com javascriptiqueryable.codeplex.com fromjs.codeple ...

  4. C语言中long类型,int类型

    long类型表示long int,一般简写为long,注意long不表示long double.虽然有时候会有sizeof(long)=sizeof(int),long int与int是不同的: 16 ...

  5. Populating Next Right Pointers in Each Node 解答

    Question Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLink ...

  6. 点滴记录——Centos 6.5 yum安装Ganglia

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701141 注:下面操作都仅仅是在一台机器上操作 1. 安装php支持  yum inst ...

  7. 多点触控插件Hammer.js

    插件描述:Hammer.js是一个开源的,轻量级的javascript库,它可以在不需要依赖其他东西的情况下识别触摸,鼠标事件. 使用方法: <script src=<span class ...

  8. JS知识点概况

    1.什么是JavaScript a)   JavaScript 被设计用来向 HTML 页面添加交互行为. b)   JavaScript 是一种脚本语言(脚本语言是一种轻量级的编程语言). c)   ...

  9. iOS获取文件路径

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p2 ...

  10. iOS之短信认证

    短信验证 现在很多的短信验证平台,我们比较常用的有移动开发者服务平台 根据短信验证文档来集成 1. 找到iOS短信验证的集成开发文档 2. 下载SDK和Demo目录结构  3. 运行Demo 4. 写 ...