LeetCode #86 Medium

Partition List

Partition List is LeetCode 86 (Medium). You get the head of a linked list and a value x. Rearrange the list so that every node with a value less than x comes before every node with a value greater than or equal to x, and return the new head.

  • Keep the original order inside each group. This is a stable partition, unlike the swap-based partition step of quicksort.
  • x does not have to appear in the list, and the list may be empty.
Constraints
  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200
linked-listtwo-pointers
Open on LeetCode ↗
02

Intuition

Build the answer as two separate chains while reading the list once: one for values below x, one for the rest. Each node is appended to the tail of its chain, so the order inside each chain is the order the nodes were read, which is exactly the stability the problem asks for.

  • Dummy heads: each chain starts at a placeholder node. Appending is then always tail.next = node, with no special case for the first node of a chain.
  • Finishing: the low chain's tail points to the first real node of the high chain, and the high chain's tail must be told it is now the end.
How to spot this pattern

"Rearrange a linked list into groups, keeping relative order" is a job for one tail pointer per group and a final join, not for swapping values.

03

Approach

Try it first

Before reading on, decide how many chains you need and what the very first append to each chain looks like. Then ask what the last node of the second chain still points to when the loop ends. Aim for O(n) time and O(1) extra space.

1

Create two dummy heads

Make small and big placeholder nodes and point the tails s and b at them. The placeholders mean an empty chain and a non-empty chain are appended to the same way.

2

Send each node to its chain

Walk the list. If the value is less than x, link the node after s and move s onto it; otherwise do the same with b. The node keeps its old next for now, which is how the walk moves on.

3

End the high chain

After the walk, set b.next = None. The last node of the high chain was usually followed by something in the original list, and if that link stays it can point back into the low chain.

4

Join and return

Set s.next = big.next so the low chain runs straight into the first real node of the high chain, then return small.next. Skipping the placeholders keeps them out of the answer.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
▶3 small, big = ListNode(), ListNode()
▶4 s, b = small, big
▶5 while head:
▶6 if head.val < x:
▶7 s.next = head
▶8 s = s.next
▶9 else:
▶10 b.next = head
▶11 b = b.next
▶12 head = head.next
▶13 b.next = None
▶14 s.next = big.next
▶15 return small.next
list143252below 3start3 or morestarttwo empty chains, each with a placeholder start
x3
Each chain begins at a placeholder node, so adding the first real node works exactly like adding any later one: link it after the tail and move the tail.
list143252below 3start13 or morestart1 < 3 → end of the low chain
node1
chainbelow x
1 is less than 3, so it goes after the current tail of the low chain. Appending at the tail keeps the low values in the order they were read.
list143252below 3start13 or morestart44 > 3 → end of the high chain
node4
chainx or more
4 is not less than 3, so it goes after the tail of the high chain, behind every high value read before it.
list143252below 3start13 or morestart433 equals 3 → end of the high chain
node3
chainx or more
Equal to x is not less than x, so it goes after the tail of the high chain, behind every high value read before it.
list143252below 3start123 or morestart432 < 3 → end of the low chain
node2
chainbelow x
2 is less than 3, so it goes after the current tail of the low chain. Appending at the tail keeps the low values in the order they were read.
list143252below 3start123 or morestart4355 > 3 → end of the high chain
node5
chainx or more
5 is not less than 3, so it goes after the tail of the high chain, behind every high value read before it.
list143252below 3start1223 or morestart4352 < 3 → end of the low chain
node2
chainbelow x
2 is less than 3, so it goes after the current tail of the low chain. Appending at the tail keeps the low values in the order they were read.
list143252below 3start1223 or morestart435next was 2 → nonecut 5's old link → it ends the list
high tail5
5 still points to 2, the node after it in the original list, which now sits in the low chain. Left alone, that link would send the finished list back into the low chain and round forever.
list143252below 3start1223 or morestart435low tail → first real node of the high chain
low tail2
The low chain's tail now points past the high placeholder to its first real node, so every value below x is followed by every other value.
result122435below 3start1223 or morestart435result: 1 → 2 → 2 → 4 → 3 → 5
return1
The answer starts at the node after the low placeholder, so neither placeholder is part of it. Within each group the order matches the input.
05

Common pitfalls

Leaving the high chain's last pointer as it was

✗ Wrong
s.next = big.next
return small.next
✓ Right
b.next = None
s.next = big.next
return small.next

For [1, 4, 3, 2, 5, 2] with x = 3, node 5 is the last high node and still points at the final 2, which is the low chain's tail. That 2 now points to 4, so the list loops 2 → 4 → 3 → 5 → 2 forever.

Putting values equal to x in the low chain

✗ Wrong
if head.val <= x:
✓ Right
if head.val < x:

Only values strictly less than x go first. For [3, 1] with x = 3 the answer is [1, 3], but with <= both nodes go low and the output stays [3, 1].

06

Edge cases

Every value is below x, or none is

One chain stays empty, so its dummy's next is null. Joining to a null high chain or starting from an empty low chain still returns the whole list in its original order.

Empty list

The loop never runs and small.next is null, which is the right answer.

07

Complexity

Time
O(n)
Space
O(1)
Each node is read once and relinked once. The two placeholders and two tails are the only extra memory, because the chains reuse the original nodes.
08

Partition List FAQ

Why not swap values like the partition step in quicksort?

Swapping values puts the right numbers on each side, but not in their original order. For [4, 3, 1] with x = 2, swapping 4 with 1 gives [1, 3, 4], while the stable answer is [1, 4, 3].

Can this be done without dummy nodes?

Yes, by keeping a separate head and tail for each chain and checking whether the head is still null before every append. It saves two small objects but adds a branch to each append and to the final join.