smilevchy's blog

Life & Study & Chasing

Leetcode_Partition List

原题链接: https://oj.leetcode.com/problems/partition-list/

public ListNode partition(ListNode head, int x) {
    if (head == null) return null;

    ListNode lastSmallerNumCursor = null;
    ListNode prev = null;
    ListNode cur = head;

    while (cur != null) {
        if (cur.val < x) {
            if (prev == null) {
                lastSmallerNumCursor = cur;
                cur = cur.next;

                continue;
            }

            prev.next = cur.next;
            if (lastSmallerNumCursor == null) {
                cur.next = head;
                head = cur;
            } else {
                cur.next = lastSmallerNumCursor.next;
                lastSmallerNumCursor.next = cur;
            }

            lastSmallerNumCursor = cur;
        }

        prev = cur;
        cur = cur.next;
    }

    return head;
}

algorithm

« Leetcode_Linked List Cycle II Leetcode_Flatten Binary Tree to Linked List »