86. Partition List

Too easy!

Define two dummy notes and heads.

定义两个虚拟节点和两个头。

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode dump1 = new ListNode(), dump2 = new ListNode();
        ListNode pre1 = dump1, pre2 = dump2;
        while(head != null) {
            if(head.val < x) {
                pre1.next = head;
                pre1 = pre1.next;
            } else {
                pre2.next = head;
                pre2 = pre2.next;
            }
            head = head.next;
        }
        pre2.next = null;
        pre1.next = dump2.next;
        return dump1.next;
    }
}

Last updated