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. 一次lenovo a390t刷机体验

    今天一朋友说自己的联想a390t手机有时候打着打着电话就没声音了,手机好像死机了一样,以前用着挺好的没什么毛病. 因为以前用刷机精灵刷过几个android手机,感觉挺简单的,只要找好对应的rom点击两 ...

  2. 多系统启动光盘制作---WIN7+WinXP+老毛桃PE工具箱

    1.工具: ⑴ Windows 7 ISO: ⑵ Windows XP ISO: ⑶ 老毛桃U盘启动盘制作工具V2013 制作得的ISO (含PE.DOS等): ⑷ UltraISO.EasyBoot ...

  3. typedef 与 define 的区别

    1.区别 (1)定义.执行时间.作用域 定义.执行时间: #define pchar char * typedef char *pchar; 定义的格式差别,显而易见的,要注意,define 是不能存 ...

  4. 导出txt格式的说明书

    /// <summary> /// 说明书 /// </summary> /// <returns></returns> public FileResu ...

  5. Freebsd的ports命令

    安装 make clean 卸载 make deinstall 重装 make reinstall 清理 make clean 列出配置单 make config 恢复默认的配置单 make rmco ...

  6. iOS陆哥开发笔记(七) (AVFoundation简单介绍)

    在AVFoundation框架中AVAudioRecorder类专门处理录音操作,支持多种音频格式. 以下是经常使用的属性和方法: 属性 说明 @property(readonly, getter=i ...

  7. containsKey使用方法

    作用是判断Map中是否有所需要的键值,下面是具体的代码: public static void main(String[] args) { Map<String, String> map ...

  8. uboot之run_command简单分析

    本文档简单分析了uboot中命令的实现.run_command函数的实现以及从uboot命令行接收并处理命令的过程. 作者: 彭东林 邮箱: pengdonglin137@163.com http:/ ...

  9. chessy 提高篇系列 阅读笔记

    java提高篇(一)—–理解java的三大特性之封装 封装的好处, 汇聚属性和方法 减少修改对 其他处的影响 控制get和set方法. java提高篇(二)—–理解java的三大特性之继承 继承的好处 ...

  10. 14 nginx 中配置 expires缓存提升网站负载

    一:nginx 中配置 expires缓存提升网站负载 对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的 ...