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下的Mysql的远程访问

    mysql的服务端[192.168.25.136] 1,在远程访问之前需先配置防火墙 service iptables stop (不推荐,可配置开通3306端口) 2,授权 mysql> gr ...

  2. cmd pyhton

    在cmd中运行python解释器: 1.同时执行多条指令,可在多条指令中间使用 & 连接 >>> print('123') &print('223') 123 223

  3. [COGS 2064]爬山

    2064. 爬山 ★☆   输入文件:mountain.in   输出文件:mountain.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 球有一天走在街上. 一个健 ...

  4. 软工团队 - 预则立&&他山之石

    软工团队 - 预则立&&他山之石 团队任务计划 时间 人员 任务 10.23-10.29 张昭锡 初拟Android代码规范 李永盛 初拟PHP代码规范 刘晨瑶 初拟Git代码规范 刘 ...

  5. python操作mysql二

    游标 游标是一种能从包括多条数据记录的结果集中每次提取一条记录的机制,游标充当指针的作用,尽管游标能遍历结果中的所有行,但它一次只指向一行,游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行 ...

  6. 死磕salt系列-salt 配置文件管理

    SLS是Salt State系统的核心,用来描述系统的目标状态,使用YAML语言书写.被用作配置文件管理. SLS文件 sls配置文件分为两种类型 top.sls 这是所有配置文件的入口 sls 这是 ...

  7. Spring(三)之Ioc、Bean、Scope讲解

    Spring容器是Spring Framework的核心.容器将创建对象,将它们连接在一起,配置它们,并管理从创建到销毁的整个生命周期.Spring容器使用DI来管理组成应用程序的组件.这些对象称为S ...

  8. C#简单实现LRU缓存

    最近跟同学吃饭扯淡的时候,由技术扯到薪资,又由薪资扯到他找工作时跟面试官是怎么扯淡拿高工资的,各种技术一顿侃,总之只要啥都了解就没问题了.谈到缓存的时候,我试探性的问了问- -你还记得LRU怎么写吗, ...

  9. C#中调用方法

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. windows 建立任务执行计划 自动执行脚本

    对于windows服务器网站如果要定时执行脚本,则需要在windows控制面板里找到 管理工具,点击任务计划程序,创建任务填写任务名称 触发器里新建触发条件,设置间隔时间 在操作项,新建触发时需要做的 ...