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. LOJ P3960 列队 树状数组 vector

    https://www.luogu.org/problemnew/show/P3960 树状数组预处理之后直接搞就可以了,也不是很好解释,反正就是一个模拟过程的暴力用树状数组维护,还挺巧妙的. 我为什 ...

  2. 【洛谷】NOIP提高组模拟赛Day1【组合数学】【贪心+背包】【网络流判断是否满流以及流量方案】

    U41568 Agent1 题目背景 2018年11月17日,中国香港将会迎来一场XM大战,是世界各地的ENLIGHTENED与RESISTANCE开战的地点,某地 的ENLIGHTENED总部也想派 ...

  3. hdoj 4450 Draw Something 水题

    Draw Something Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. Spring_Task初探(注解,XML配置)

    这几天想写一个动态添加任务项目找了找Spring下的自带定时功能发现还真有,然后网上找了找资料写了个demo 写了两种方式来执行定时的任务(XML配置和注解) 先建两个普通的任务类(XML配置调用的任 ...

  5. 一个java高级工程师的进阶之路【转】

    宏观方面 一. JAVA.要想成为JAVA(高级)工程师肯定要学习JAVA.一般的程序员或许只需知道一些JAVA的语法结构就可以应付了.但要成为JAVA(高级) 工程师,您要对JAVA做比较深入的研究 ...

  6. python类型比较的3种方式(转)

    通过types模块的类成员来判断,其实所有python中的类型都是这个types模块中类型的实例. import types type(x) is types.IntType # 判断是否int 类型 ...

  7. Windows Server 2016 Essentials试用

    下载地址: Windows Server 2016 Essentials (x64) SHA-1:        6E1D1880873157ADCEF3D74363308A95DC89103D ed ...

  8. Adding an instance to a MEF container

    How can you add an already created instance to a MEF container/cataloge to use when resolving Import ...

  9. redhat/centos使用service控制启动与关闭

    原文地址: http://guodong810.blog.51cto.com/4046313/1285353 有时,我们自己安装了某个软件时,想让对这个服务更加容易的控制,在redhat/centos ...

  10. 1. python 字符串简介与常用函数

    1. python中的字符串简介与常用函数 在python中,字符串变成了一个强大的处理工具集,他是不可变的,也就是说字符串包含字符与字符的顺序,他不可以原处修改 字符串是我们后面需要学习的稍大一点的 ...