https://oj.leetcode.com/problems/partition-list/

http://blog.csdn.net/linhuanmars/article/details/24446871

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        
        if (head == null)
            return head;
            
        ListNode dummyhead = new ListNode(0);
        dummyhead.next = head;
        
        ListNode pre = dummyhead;
        ListNode lastmin = dummyhead;
        
        while (pre.next != null)
        {
            ListNode node = pre.next;
            if (node.val >= x)
            {
                pre = pre.next;
            }
            else
            {
                // Tricky.
                if (pre == lastmin)
                {
                    // No change
                    pre = pre.next;
                    lastmin = lastmin.next;
                }
                else
                {
                    // Move node after lastmin;
                    ListNode next = node.next;
                    pre.next = next;
                    ListNode lastminnext = lastmin.next;
                    node.next = lastmin.next;
                    lastmin.next = node;
                    lastmin = node;
                }
            }
        }
        
        return dummyhead.next;
    }
}