Description

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.

Example

Given 1->4->3->2->5->2->null and x = 3,
return 1->2->2->4->3->5->null.

看到“partition”,还以为是快速排序呢。。。题目的意思是,给一个链表,再给一个数。将链表中的数分为两个类:小于x、大于等于x。返回值为该类型的ListNode,将小于x的放前面,大于等于x的,放在后面。先贴给图:

哈哈哈,其实思路也挺简单的,不知道怎么就成最快的了。

思路:按照我分析的,将原链表拆分成两个链表,最后再合并起来。代码如下:

/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param head: The first node of linked list
* @param x: An integer
* @return: A ListNode
*/
public ListNode partition(ListNode head, int x) {
// write your code here
//放值小于x的结点,lp为插入low链表时的尾指针
ListNode low=null;
ListNode lp=low;
//放值大于等于x的结点,hp为插入high链表时的尾指针
ListNode high=null;
ListNode hp=high;
//p为循环的游标
ListNode p=head;
while(p!=null){
if(p.val<x){
//注意,low是不是空的插入代码是不同的,如果low为空
//直接lp.next会报错
if(low==null){
low=p;
lp=low;
p=p.next;
lp.next=null;
}else{
lp.next=p;
p=p.next;
lp=lp.next;
lp.next=null;
}
}else{
if(high==null){
high=p;
hp=high;
p=p.next;
hp.next=null;
}else{
hp.next=p;
p=p.next;
hp=hp.next;
hp.next=null;
}
}
}
//如果没有比x小的
if(low==null){
return high;
}
//如果没有大于等于x的
if(high==null){
return low;
}
//如果都有
lp.next=high;
return low;
}
}

96. Partition List [easy]的更多相关文章

  1. kafka之partition分区及副本replica升级

    修改kafka的partition分区 bin/kafka-topics.sh --zookeeper datacollect-2:2181 --alter --partitions 3 --topi ...

  2. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. ORACLE分区--表分区

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

  5. 转:Oracle表分区

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

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5

    .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...

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

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

  9. kafka集群操作指南

    目录 kafka集群操作指南 (一)单机版安装 (二)集群安装 (三)集群启停操作 (四)topic相关的操作 (五)某个broker挂掉,本机器可重启 (六)某个broker挂掉且无法重启,需要其它 ...

随机推荐

  1. Linux traceroute命令详解

    traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样, ...

  2. redis3.2.9编译安装

    Redis 3.2.9 安装 Redis 3.2.9 编译安装 1,   安装相关软件包 2,   下载redis源码包 wget http://source.goyun.org:8000/sourc ...

  3. ZT 获得/修改共享互斥量属性:pthread_mutexattr_t

    bbs.chinaunix.net/thread-965755-1-1.html 5.   获得/修改共享互斥量属性:    #include<pthread.h>    intpthre ...

  4. LINQ入门与标准查询运算符

    LINQ的体系结构 查询表达式的完整语法 一.查询表达式必须以from子句开头,以select 或group子句结束.中间可以使用where,orderby ,join,let和其他子句.具有“延迟计 ...

  5. python28 excel读取模块xlrd

    安装: pip install xlrd 简单使用: import xlrd book = xlrd.open_workbook(r'C:\Users\dinghanhua\Desktop\yqqap ...

  6. IOS .a静态库的和.framework制作

    什么是库? 库是程序代码的集合,是共享程序代码的一种方式 根据源代码的公开情况,库可以分为2种类型 开源库 公开源代码,能看到具体实现 比如SDWebImage.AFNetworking 闭源库 不公 ...

  7. 第04章-VTK基础(7)

    [译者:这个系列教程是以Kitware公司出版的<VTK User's Guide -11th edition>一书作的中文翻译(出版时间2010年.ISBN: 978-1-930934- ...

  8. 组合测试(Combinatorial Test)/配对测试 (pairwise)

    组合测试方法:配对测试实践 实施组合测试 常用的Pairwise工具集:http://www.pairwise.org/tools.asp 成对测试(Pairwise Testing)又称结对测试.两 ...

  9. linq查询语法和方法-簡單用法

    來自:http://www.cnblogs.com/knowledgesea/p/3897665.html 1.简单的linq语法 //1 var ss = from r in db.Am_recPr ...

  10. (转)解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误

    重新启动服务器,访问web服务发现无法浏览啦!登陆服务器之后进到nginx使用./nginx -s reload重新读取配置文件,发现报nginx: [error] open() "/usr ...