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. ASP.NET Core 上传多文件 超简单教程

    示例源码下载地址 https://qcloud.coding.net/api/project/3915794/files/4463836/download 项目地址 https://dev.tence ...

  2. 申请MVP奖励时的小Tips

    大家新年好,今天MSPrecious为大家带来一些申请MVP奖励时的小Tips.   本文分为三个部分 MVP是什么 如何申请MVP 申请MVP需要注意的事项 MVP是什么? 我想,点进来看这篇文章的 ...

  3. 使用事务和SqlBulkCopy批量插入数据

    SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据有效批量的加载到SQL Server表中的功能 ...

  4. 深入浅出SharePoint2012——安装Report Service

    安装顺序 Microsoft .NET Framework 3.5 SP1 report service installation,pls SQLServer2008R2SP1-KB2528583-x ...

  5. 使用信号进行同步 sem_post

    使用信号进行同步 信号是 E. W. Dijkstra 在二十世纪六十年代末设计的一种编程架构.Dijkstra 的模型与铁路操作有关:假设某段铁路是单线的,因此一次只允许一列火车通过. 信号将用于同 ...

  6. 使用JAVA进行排序

    利用JAVA完成排序 当我们在进行数据库进行查询的时候,当需要按某个字段来进行排序的时候,可以使用SQL语句来完成排序,可以升序,也可以降序.JAVA中的Collections类也可以完成这种操作,S ...

  7. angularJs中的模块化操作

    一.全局的写法 有可能会跟其他程序有冲突 <!DOCTYPE HTML> <html ng-app="myApp"> <head> <me ...

  8. SOJ 1017 Power of Cryptography 库函数精度

    Background Current work in cryptography involves (among other things) large prime numbers and comput ...

  9. Kali-linux使用Maltego收集信息

    Maltego是一个开源的漏洞评估工具,它主要用于论证一个网络内单点故障的复杂性和严重性.该工具能够聚集来自内部和外部资源的信息,并且提供一个清晰的漏洞分析界面.本节将使用Kali Linux操作系统 ...

  10. Monkeyrunner命令

    1.使用Monkeyrunner脚本命令时,需要导入模块才能使用模块的脚本命令,Monkeyrunner的常用模块有 MonkeyRunner,MonkeyDevice,MonkeyImage,Mon ...