Partition List

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 的结点。

注意:记住两个表尾,中间不能将其 next 指针设置为空。最后将存小值的表尾链接在另一个前面,两一个表尾 next 置为 NULL.

/**
* 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 *head1, *head2, *tail1, *tail2;
head1 = head2 = tail1 = tail2 = NULL;
while(head) {
if(head->val >= x) {
if(head2 == NULL) head2 = tail2 = head;
else { tail2->next = head; tail2 = tail2->next; }
} else {
if(head1 == NULL) head1 = tail1 = head;
else { tail1->next = head; tail1 = tail1->next; }
}
head = head->next;
}
if(head2) tail2->next = NULL;
if(head1) tail1->next = head2;
else head1 = head2;
return head1;
}
};

46. Partition List的更多相关文章

  1. LeedCde 题解目录

    1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...

  2. ORACLE分区--表分区

    .love_flying_snow Oracle表分区 Oracle . 废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库 ...

  3. 转:Oracle表分区

    Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1. create table graderecord 2. ...

  4. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区(转载)

    一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1 create table graderecord 2 ( 3 sno varchar2(10), 4 sname varcha ...

  5. 每天一个linux命令(46):vmstat命令

    vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行深 ...

  6. PLSQL_性能优化系列09_Oracle Partition Table数据分区表

    2014-08-22 Created By BaoXinjian

  7. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  8. PAT1113: Integer Set Partition

    1113. Integer Set Partition (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. A1113. Integer Set Partition

    Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...

随机推荐

  1. 关闭缓存和mmu(转)

    当设置完时钟分频以后,uboot就会执行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu,然后调用lowlevel_init函数进行系统总线的初始化. 为什么启动的时候,需要 ...

  2. 项目中必须知道的关于CSS+DIV的常识

    根据模块化的思想,将目录划分为html,css,image三大部分. css部分:(base.css.globa.css和mod文件夹)1.base.css放置的是reset,clearfix等基础类 ...

  3. ArrayList与普通数组的区别

    import java.util.ArrayList; public class Shuzuqubie { public static void main(String[] args){ String ...

  4. C#获取字符首字母

    ///<summary> /// 获取字符首字母 /// </summary> public static string GetPyChar(string c) { if (s ...

  5. 【转载】分享一些Qt学习资源,欢迎下载

    资源来源:http://bbs.csdn.net/topics/390358737 经过我一翻整理,把一些我收集到的Qt学习资源分享给大家,主要适合新手,老鸟可以直接忽略我.要说明一下,很多资源都是在 ...

  6. poj 3463 最短路与次短路的方案数求解

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8968   Accepted: 3139 Descr ...

  7. php部分---一个分页类、用法

    1.分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...

  8. [BZOJ 3209]花神的数论题

    一道简单的数位 dp 题 但是脑子里只有 __builtin_popcountll 了呢(自重) 看完题解后很快就理解了,而且有一种这么简单的题居然没想到做法真是不应该唉~的感觉 用 f[i] 表示 ...

  9. 论文笔记之:Co-saliency Detection via A Self-paced Multiple-instance Learning Framework

    Co-saliency Detection via A Self-paced Multiple-instance Learning Framework  T-PAMI  2016  摘要:Co-sal ...

  10. Unity3d 引擎原理详细介绍

    体系结构 为了更好地理解游戏的软件架构和对象模型,它获得更好的外观仅有一名Unity3D的游戏引擎和编辑器是非常有用的,它的主要原则. Unity3D 引擎 Unity3D的是一个屡获殊荣的工具,用于 ...