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 两个链表 再连起来  还是用伪头部

class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode large(), small();
ListNode * l = &large;
ListNode * s = &small; while(head != NULL)
{
if(head->val < x)
{
s = s->next = head;
}
else
{
l = l->next = head;
}
head = head->next;
} l->next = NULL;
s->next = large.next;
return small.next;
}
};

【leetcode】Partition List(middle)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. 深入理解Javascript面向对象编程

    深入理解Javascript面向对象编程 阅读目录 一:理解构造函数原型(prototype)机制 二:理解原型域链的概念 三:理解原型继承机制 四:理解使用类继承(继承的更好的方案) 五:建议使用封 ...

  2. uva10870 递推关系Recurrences

    Consider recurrent functions of the following form:f(n) = a1f(n - 1) + a2f(n - 2) + a3f(n - 3) + : : ...

  3. notepad++和sublime 常用插件及主题

    sublime: 常用主题有: Pastels on Dark Monokai Zenburnsque 常用插件有 Anaconda Package Control Side Bar ConvertT ...

  4. 【C语言入门教程】3.2 数据的输入 与 输出

    在程序的运行过程中,通常需要用户输入一些数据,而程序运算所得到的计算结果等又需要输出给用户,由此实现人与计算机之间的交互.所以在程序设计中,输入输出语句是一类必不可少的重要语句.在 C 语言中,没有专 ...

  5. linux 下安装ftp服务器

    最后重启    # service vsftpd restart   1.查看是否安装vsftp rpm -qa | grep ftp 如果出现    vsftpd-2.0.5-16.el5_5.1 ...

  6. autolayout的各种坑

    xocde7的autolayout 在viewDidLoad之前, 使用frame改变布局是没有用的, 简单的视图才可以使用autolayout, 稍微复杂写的都要使用代码来编写 获取当前view的宽 ...

  7. 快速排序python实现

    #--×--coding:utf-8-*- def main(): nlist = [] while 1: tmp = raw_input("Please input your elemen ...

  8. ionic的页面直接的跳转

    $state.go页面不刷新数据 假如进入market/beian/add添加数据,保存提交后回退market/beian列表页,没有自动更新数据,必须得手动下拉刷新才会出来 $state.go(&q ...

  9. js之词法分析

    词法分析 词法分析: 先分析参数: 再分析变量声明: 分析函数声明: 一个函数能使用的局部变量,就从上面的3步分析而来 具体步骤: 1.函数运行前的一瞬间,生成 Active Object(活动对象) ...

  10. ZJOIDay2T1 BB题解

    讲道理我是调不出来了... 考虑对序列按下标维护每个节点最后的树. 那么 改操作点 - 把一段连续的节点改父亲 加点/删点(注意拆成两个操作了) 插儿子 那么用seg维护一下下标, 用ETT维护Dep ...