YSMull
<-- algorithm



原题链接

遍历链表的过程中,把结点分别收集到两个链表中。

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode less = new ListNode(0);
        ListNode greater = new ListNode(0);
        ListNode p = less;
        ListNode q = greater;
        while (head != null) {
            ListNode t = head.next;
            head.next = null;
            if (head.val < x) {
                p.next = head;
                p = p.next;
            } else {
                q.next = head;
                q = q.next;
            }
            head = t;
        }
        p.next = greater.next;
        return less.next;
    }
}