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. ios开发 - 获取从http上下载文件的大小

    - (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response{ NSHTTPURL ...

  2. OFV.msi是什么 为什么更新时无法安装

    在网络上搜索了下 这个是Microsoft Office 文件验证加载项,微软提供了一个单独的文件:http://www.microsoft.com/downloads/zh-cn/confirmat ...

  3. selenium遇到不可编辑input和隐藏input如何赋值

    js = 'document.getElementsByName("m:csr_mt_gnccspjclfbxd:bmshldID")[0].type="text&quo ...

  4. mysql赋给用户权限grant all privileges on

    查看mysql用户表的结构,Field项都是各类权限限制 Host限制登录的IP,User限制登录的用户,Delete_priv限制删除权限,Grant_priv限制权限授予,Super_priv为超 ...

  5. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. git 指令如何撤销一次merge

    在使用git指令时难免会发生错误的merge的情况,那么如何在这种情况下回退到错误发生之前的情况? git reflog 指令显示历史的操作 4457e43 HEAD@{0}: reset: movi ...

  7. 2016.8.22 Axure两级下拉框联动的实现

    刚学Axure,有些很简单的东西都要弄很久,但是弄出来的总归是很开心的. 参考来自:实现省市县下拉框的三级联动 http://www.woshipm.com/rp/348795.html/commen ...

  8. Java程序员新手老手都离不开八大开发工具

    以下这8个工具,从代码构建到错误挤压,覆盖Java开发的全域.学习这些工具可以帮助你改善代码质量,成为一个更高效的Java开发人员.Java这个大世界中正在不断涌现新的工具.实用程序和库.如果你的首选 ...

  9. opengl加载多个3ds模型失败记

    VC6 下载 http://blog.csdn.net/bcbobo21cn/article/details/44200205 opengl环境配置 http://blog.csdn.net/bcbo ...

  10. 使用FDTemplateLayout框架打造个性App

    效果展示 project下载地址 · 进入构建结构 首先我们新建一个project 接下来我们拖进来一个Table View Controller,将Storyboard Entry Point指向我 ...