↓Skip to main content
  1. LeetCode/

LeetCode 86: Partition List

·2 mins· ·
LeetCode Medium Linked-List Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-26

  • Submissions: Runtime Error → Wrong Answer → Accepted ×2 → Runtime Error → Wrong Answer → Accepted

Approach
#

The relative order must be kept, so scan once, appending nodes below x after the first dummy and the rest after the second (Linked List Template 1). Join the first part’s tail to the second part’s head, and set the second part’s tail to None, or it may still point into the old list and form a cycle.

My Attempts
#

Attempt 1 moved the dummy itself as the tail, so the final l1.next was not the head: [1], x = 2 returned nothing. Later rewrites used an undefined variable target, and one failed when everything landed in one part (raising on an empty list).

Solution
#

class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        small = s = ListNode()
        big = b = ListNode()
        while head:
            if head.val < x:
                s.next = s = head
            else:
                b.next = b = head
            head = head.next
        b.next = None
        s.next = big.next
        return small.next

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Keep the dummies separate from the moving tail pointers.
  • Set the second part’s tail to None.

Interview Explanation
#

Two dummies collect nodes below x and the rest in order; then I join them and terminate the second. The original order is preserved. Time O(n).

Related

LeetCode 82: Remove Duplicates from Sorted List II
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 82 note: every duplicated value goes, the head included, so use a dummy; on a duplicate skip to the next different value and link prev.next past it
LeetCode 19: Remove Nth Node From End of List
·2 mins
LeetCode Medium Linked-List Two-Pointers
LeetCode 19 note: from a dummy, move fast n + 1 steps ahead, then move both to the end; slow stops before the node to delete
LeetCode 61: Rotate List
·1 min
LeetCode Medium Linked-List Two-Pointers
LeetCode 61 note: count the length and close a ring, take k %= n, walk n - k - 1 steps from the head to the new tail, and break there
LeetCode 75: Sort Colors
·2 mins
LeetCode Medium Array Two-Pointers Sorting Quicksort
LeetCode 75 note: the Dutch flag, with 0s left of low and 2s right of high; after swapping a 2 to high, do not advance, since the swapped-in value is unchecked
LeetCode 80: Remove Duplicates from Sorted Array II
·1 min
LeetCode Medium Array Two-Pointers
LeetCode 80 note: keep each value at most twice by comparing with nums[k - 2], two slots back from the write position
LeetCode 24: Swap Nodes in Pairs
·2 mins
LeetCode Medium Linked-List Recursion
LeetCode 24 note: a dummy plus pairs, rewiring prev.next, a.next and b.next together; swap nodes, not values