Partition List

Medium

Topics
Linked ListTwo Pointers

Given the head of a linked list and a value k, partition it so that all nodes with value less than k come before nodes with value greater than or equal to k. Preserve the original relative order within each part. (LeetCode calls this parameter x.)

Example 1

before143252null
after122435null
less than 3
Nodes below 3 come first, and relative order is preserved inside both groups — so this is a stable partition, not a sort.
Linked list values from head to tail.
Input:  head = [1,4,3,2,5,2], k = 3
Output: [1,2,2,4,3,5]

Example 2

before21null
after12null
less than 2
Input:  head = [2,1], k = 2
Output: [1,2]

Constraints

  • 0 <= number of nodes <= 200
  • -100 <= Node.val <= 100
  • -200 <= k <= 200
Editorial — intuition, approach, complexity and pitfalls

Intuition

Build two chains while walking the input once: one for values below k, one for the rest. Appending in input order means relative order is preserved for free — there is no reordering step to get wrong.

Two dummy heads remove every special case around the first node of each chain, including the case where one chain stays empty. At the end, join the two and return.

Approach

  1. Create dummy heads for the less and greater-or-equal chains, with tails pointing at them.
  2. Walk the input, appending each node to whichever chain its value selects.
  3. Terminate the greater chain by setting its tail's next to null.
  4. Point the less chain's tail at the greater chain's first real node and return the less chain.

Dry Run

input  1 -> 4 -> 3 -> 2 -> 5 -> 2      k = 3

less chain      1 -> 2 -> 2
greater chain   4 -> 3 -> 5
join            1 -> 2 -> 2 -> 4 -> 3 -> 5

note that 4 -> 3 keeps its input order; the greater chain is not sorted

Complexity

  • Time — O(n): one pass, constant work per node.
  • Space — O(1): two dummy nodes and four pointers. No node is copied; the original nodes are relinked in place.

Common Pitfalls

  • Omitting the g.next = None termination. The last node of the greater chain still points wherever it pointed in the input, which closes a cycle: on [1,4,3,2,5,2] with k = 3 the result walks 1, 2, 2, 4, 3, 5, 2, 4, 3, 5, 2, 4, and never ends. This is the defect that makes the problem worth asking.
  • Using <= instead of <. Nodes equal to k belong in the second part.
  • Building the chains from the real first nodes instead of dummies, which forces a null check on every append and a special case when a chain ends up empty.
  • Swapping values to avoid relinking. It breaks stability within a part, which the problem requires.
  • Sorting, or partitioning as in quicksort. Quicksort's partition is not stable, and stability is the requirement here.
  • Forgetting that the less tail must be relinked too. If every node went to the greater chain, the less tail is still the dummy and the join is what makes the function return the right head.

Full Solution — all languages

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def partition(self, head, k):
        less = ListNode(0)
        greater = ListNode(0)
        l, g = less, greater
        cur = head
        while cur:
            if cur.val < k:
                l.next = cur
                l = cur
            else:
                g.next = cur
                g = cur
            cur = cur.next
        g.next = None
        l.next = greater.next
        return less.next
SensAI
Beta
Tap the mic to stop voice mode