Partition List - Problem
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Input & Output
Example 1 — Basic Partition
$
Input:
head = [1,4,3,2,5,2], x = 3
›
Output:
[1,2,2,4,3,5]
💡 Note:
Nodes with values less than 3 (1, 2, 2) come before nodes greater than or equal to 3 (4, 3, 5). Original relative order is preserved within each partition.
Example 2 — Single Element
$
Input:
head = [2], x = 1
›
Output:
[2]
💡 Note:
Single node with value 2 is greater than x=1, so it remains in its position.
Example 3 — All Elements Same Side
$
Input:
head = [1,1,1], x = 5
›
Output:
[1,1,1]
💡 Note:
All nodes have values less than x=5, so the list remains unchanged.
Constraints
- The number of nodes in the list is in the range [0, 200]
- -100 ≤ Node.val ≤ 100
- -200 ≤ x ≤ 200
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original linked list with mixed values and partition value x
2
Partition
Separate nodes into two groups: < x and >= x
3
Output
Reconnected list with all < x nodes before >= x nodes
Key Takeaway
🎯 Key Insight: Use two pointers to build separate lists for nodes < x and >= x, then connect them
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code