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,
return 1->2->2->4->3->5.

题目要求:

给一个链表和一个数值x,将所有小于x的结点放在所有大于或等于x的结点的前面。

要求不改变原链表结点的相对顺序。

解题思路:

在数组partition中,一般是通过首尾两个指针来进行前后遍历以及交换;

而在链表中,不需要进行元素的交换,可以通过创建两个新的头结点指针,来分别指向小于x的结点和大于等于x的结点,遍历结束之后,再将两个新的链表重新连接起来。

代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *left_head=NULL,*left_tail=NULL;
ListNode *right_head=NULL,*right_tail=NULL;
ListNode *p=head; while(p){
if(p->val<x){
if(left_tail){
left_tail->next=p;
left_tail=left_tail->next;
}
else
left_head=left_tail=p;
}
else{
if(right_tail){
right_tail->next=p;
right_tail=right_tail->next;
}
else
right_head=right_tail=p;
}
p=p->next;
} if(right_tail)
right_tail->next=NULL;
if(left_tail)
left_tail->next=right_head; return left_head?left_head:right_head;
}
};

(LeetCode 86)Partition List的更多相关文章

  1. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  2. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. LeetCode(86) Partition List

    题目 Given a linked list and a value x, partition it such that all nodes less than x come before nodes ...

  4. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  5. 不使用循环或递归判断一个数是否为3的幂(leetcode 326)

    326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...

  6. python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)

    1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...

  7. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

  8. (LeetCode 153)Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. (LeetCode 160)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

随机推荐

  1. [Luogu5106]dkw的lcm

    https://minamoto.blog.luogu.org/solution-p5106 容易想到枚举质因子及其次数计算其贡献,容斥计算$\varphi(p^i)$的次方数. #include&l ...

  2. 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio

    题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...

  3. JavaScript惰性函数定义

    函数是js世界的一等公民,js的动态性.易变性在函数的应用上,体现的淋漓尽致.做为参数,做为返回值等,正是函数这些特性,使得js开发变的有趣. 下面就阐述一下,js一个有趣的应用--惰性函数定义(La ...

  4. ROS知识(12)----cv_bridge依赖opencv版本的问题

    cv_bridge默认依赖的oencv版本是2.4.8,如果安装了新的opencv版本,比如2.4.11,那么在编译cv_bridge时候会提示无法找到opencv 2.4.8.so的库. 为解决这个 ...

  5. Get started with IDA and disassembly SH7058

    http://www.romraider.com/forum/viewtopic.php?f=25&t=6303 All of the 16-bit guidance in the follo ...

  6. Using PWM Output as a Digital-to-Analog Converter

    http://www.ti.com/lit/an/spraa88a/spraa88a.pdf http://www.ti.com/litv/zip/spraa88a The high-resoluti ...

  7. .NET的堆和栈02,值类型和引用类型参数传递以及内存分配

    在" .NET的堆和栈01,基本概念.值类型内存分配"中,了解了"堆"和"栈"的基本概念,以及值类型的内存分配.我们知道:当执行一个方法的时 ...

  8. mORMot

    mORMot GITHUB: https://github.com/synopse/mORMot Synopse mORMot framework An Open Source Client-Serv ...

  9. Android调用手机中的应用市场,去评分的功能实现

    在我们常常使用的软件当中,我们经常可以看到在软件的设置界面,有一个功能那就是去评分的功能,只要我们一点击“去评分”就会调用手机中的应用市场软件.一开始我以为这个功能的实现是要遍历整个手机中的软件包名, ...

  10. Extjs NumberField 开始值 不能大于 结束值

    Ext.apply(Ext.form.VTypes,{ numberrange: function(val, field) { var num = parseFloat(val); if (field ...