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. Xocde 自动注释插件

    github 地址 https://github.com/onevcat/VVDocumenter-Xcode   可以对xcode方法进行类似java那样的自动注释 源码下载下后编译运行一次  xo ...

  2. ios文件管理

    <Application_Home>/AppName.app This is the bundle directory containing the applicationitself. ...

  3. 关于Android内存优化你应该知道的一切

    介绍 在Android系统中,内存分配与释放分配在一定程度上会影响App性能的—鉴于其使用的是类似于Java的GC回收机制,因此系统会以消耗一定的效率为代价,进行垃圾回收. 在中国有句老话:”由俭入奢 ...

  4. Hadoop一些问题总结

    1.运行mr程序出错 connecting to resoucemanager retrying .... retrying ..... 原因是没有启动yarn或者启动失败 2.初始化工作目录结构 h ...

  5. 转:代码管理技巧——两步创建本地SVN服务器图文教程

    from: http://www.cnblogs.com/tianhonghui/archive/2012/07/22/2603454.html   当我们进行开发的时候,不论是独立开发还是处在团队中 ...

  6. vuex 中关于 mapMutations 的作用

    mapMutations 工具函数会将 store 中的 commit 方法映射到组件的 methods 中.和 mapActions 的功能几乎一样,我们来直接看它的实现: export funct ...

  7. C/C++中作用域详解(转)

    作用域规则告诉我们一个变量的有效范围,它在哪儿创建,在哪儿销毁(也就是说超出了作用域).变量的有效作用域从它的定义点开始,到和定义变量之前最邻近的开括号配对的第一个闭括号.也就是说,作用域由变量所在的 ...

  8. Cloudera

    官方文档: http://www.cloudera.com/content/cloudera/en/documentation/core/latest/ 博客教程 http://www.wangyon ...

  9. cookie-小总结吧

    写入common.js文件,其他页面调用即可: //添加cookie值 function addcookie(name, value, days) { days = days || 0; var ex ...

  10. Web前端学习攻略

    HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础: Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMAscript ...