Linked List Partitioner - Problem
Given a linked list and a value x, partition it such that all nodes with values less than x come before all nodes with values greater than or equal to x.
Important: You should preserve the original relative order of the nodes in each of the two partitions.
Return the head of the modified linked list.
Input & Output
Example 1 — Basic Partition
$
Input:
head = [1,4,3,2,5,2], x = 3
›
Output:
[1,2,2,4,3,5]
💡 Note:
Values less than 3 (1,2,2) come first, followed by values >= 3 (4,3,5). Original relative order is preserved within each group.
Example 2 — All Values Greater
$
Input:
head = [2,1], x = 2
›
Output:
[1,2]
💡 Note:
Only value 1 is less than 2, so it comes first, followed by value 2.
Example 3 — Single Node
$
Input:
head = [1], x = 2
›
Output:
[1]
💡 Note:
Single node case: 1 < 2, 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code