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的第一个指针即可,JAVA实现如下:

public ListNode partition(ListNode head, int x) {
if (head == null || head.next == null)
return head;
ListNode temp = head, firstMin = head;
if (head.val >= x) {
while (firstMin.next != null) {
if (firstMin.next.val < x) {
head = firstMin.next;
firstMin.next = firstMin.next.next;
head.next = temp;
break;
}
firstMin = firstMin.next;
}
}
if (head.val >= x)
return head;
firstMin = head;
temp=head.next; while (temp != null && temp.val < x) {
firstMin = firstMin.next;
temp = temp.next;
}
if(temp==null)
return head;
ListNode firstMax=temp,lastMax=temp;
temp=temp.next;
while(temp!=null){
if(temp.val<x){
firstMin.next=temp;
firstMin=firstMin.next;
}else{
lastMax.next=temp;
lastMax=lastMax.next;
}
temp=temp.next;
}
firstMin.next=firstMax;
lastMax.next=null;
return head;
}

其实如果创建一个ListNode result指向head可以减少代码长度,JAVA实现如下:

    public ListNode partition(ListNode head, int x) {
ListNode result = new ListNode(0);
result.next = head;
ListNode cur = result, lastMin, firstMax;
while (cur.next != null && cur.next.val < x)
cur = cur.next;
lastMin = cur;
firstMax = cur.next;
while (cur.next != null) {
if (cur.next.val < x) {
lastMin.next = cur.next;
lastMin = lastMin.next;
cur.next = cur.next.next;
lastMin.next = firstMax;
} else
cur = cur.next;
}
return result.next;
}

Java for LeetCode 086的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  4. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  5. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. Java for LeetCode 153 Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Beginning Auto Layout Tutorial in iOS 7: Part 2

    Auto Layout to the rescue! 接下来就看看如何使用Auto Layout来实现这个效果. 首先移除viewWillLayoutSubviews方法,选择Main.storybo ...

  2. hdu1061(C++)

    简单的找规律,不妨设N=10*x+a(a=N%10),那么N^N=(10*x+a)^N,用二项式展开定理可以知道N^N%10=a^N%10; 由于0<a<10,打表a^1,a^2,a^3, ...

  3. HDU 5360 Hiking(优先队列)

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  4. node.js之http-server

    我们有时候会遇到这种情况,一个html文件在本地打开时,测试平常的功能还行,但是,一涉及到ajax请求,就算你是请求本地的json文件,他都会涉及到跨域的问题,浏览器本身就限制了本地打开时,不允许跨域 ...

  5. 白话http请求

    http接口测试和使用,首先要了解什么是http请求: http请求通俗讲就是把客户端的东西通过http协议发送到服务端,服务端根据http协议的定义解析客户端发过 来的东西! http请求中常用到的 ...

  6. IP反查网站,ip反查接口,旁站查询接口大全,通过IP查域名汇总:

    http://cn.bing.com/search?q=ip%3A220.181.111.85     http://dns.aizhan.com/?q=www.baidu.com     http: ...

  7. CentOS下配置iptables防火墙 linux NAT(iptables)配置

    CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...

  8. java 逻辑运算符 短路(条件操作)

    两个数字计算时都会先把数字转换成二进制后再进行换算,二进制就是由0和1组成的数字  http://yxwang0615.iteye.com/blog/1084288    

  9. 关于meta标签的name="viewport" 概述

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...

  10. PHP第四课 了解经常使用的函数

    学习概要: 一.语言结构 二.自己定义函数 三.变量作用域 四.静态变量 五.函数返回值 六.參数 七.默认參数 八.引用參数 九.可变个数函数 十.回调函数 十一.变量函数 十二.递归函数 十三.文 ...