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.
xdoes not have to appear in the list, and the list may be empty.
- The number of nodes in the list is in the range [0, 200].
- -100 <= Node.val <= 100
- -200 <= x <= 200
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.
"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.
Approach
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.
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.
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.
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.
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.
Code in Python, C++ and Java
Common pitfalls
Leaving the high chain's last pointer as it was
s.next = big.next return small.next
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
if head.val <= x:
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].
Edge cases
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.
The loop never runs and small.next is null, which is the right answer.
Complexity
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.