Skip to content
BytePatterns

Partition Around Value

MediumLinked Lists#dummy-node#list-splitting~30m

Problem

Given the head of a singly linked list and a pivot value, rearrange the nodes so every node holding a value below the pivot comes before every node holding a value at or above it. Inside each of the two groups the nodes keep the order they had originally. The pivot itself need not appear in the list.

Examples

Input:  head = 1 -> 4 -> 3 -> 2 -> 5 -> 2, pivot = 3
Output: 1 -> 2 -> 2 -> 4 -> 3 -> 5
Why:    both groups keep their original internal order
Input:  head = 2 -> 1, pivot = 2
Output: 1 -> 2
Why:    a node equal to the pivot belongs to the upper group
Input:  head = empty, pivot = 0
Output: empty
Why:    edge case, there is nothing to partition

Hints

0 / 3

Stuck on the idea rather than the code? Traversal and Search covers it.