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. Contest Reviews(Updating)

    现在每天至少一套题又不太想写题解…… 那就开个坑总结下每场的失误和特定题目的技巧吧 2018.8.25[ZROI] T3传送门 T1:找规律找崩了…… 最好不要一上来就钻进大讨论,先想有没有普适规律 ...

  2. python及其模块下载集合

    1)python平台 https://www.python.org/downloads/ 2)打包工具 cx-freeze(python3以上版本打包工具) http://cx-freeze.sour ...

  3. Codeforces Beta Round #37 B. Computer Game 暴力 贪心

    B. Computer Game 题目连接: http://www.codeforces.com/contest/37/problem/B Description Vasya's elder brot ...

  4. hdoj 5120 Intersection 圆环面积求交

    Intersection Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tota ...

  5. UVALive 4868 Palindrometer 暴力

    F - Palindrometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  6. 怎么样退出vi/vim编辑器

    怎么样退出vi/vim编辑器 先按   ESC   然后输入  w  q  :wq 就退出来了

  7. IDC门外汉-单线、双线、智能多线、BGP的区别

    一.单线在此不多说,不是电信,就是网通机房,是,2005年前的机房情况: 二.双线:上般是从2004年到2008年用此方法较多,此今还有不少在用此法如下: 双线主机 有单IP和单网卡双IP地址,双网卡 ...

  8. MVC到底使用哪种方式传递Model,在ViewData、ViewBag、PartialView、TempData、ViewModel、Tuple之间取舍

    在"MVC控制器传递多个Model到视图,使用ViewData, ViewBag, 部分视图, TempData, ViewModel, Tuple"中,体验了使用不同的方式传递多 ...

  9. RobotFramework自动化2-自定义关键字

    前言 有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.Selenium2library提供了Get Webelements 关键字,用于定位一组元素 以百 ...

  10. 关于 json 单引号和双引号区别--请使用双引号

    双引号才是json 的标准,单引号是不规范的(虽然在js 中是行的通的)! 由于某些原因,在将js json数据传到as 中处理的时候出现了“无效的json解析输入” ,于是查找了一下js参数是否有问 ...