Partition List
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.
Example 1
[1] -> [4] -> [3] -> [2] -> [5] -> [2] -> null --- [1] -> [2] -> [2] -> [4] -> [3] -> [5] -> null
Input
head = [1,4,3,2,5,2], x = 3Output
[1,2,2,4,3,5]Nodes with values less than 3,
[1, 2, 2], come before the remaining nodes while preserving relative order within each partition.Example 2
[2] -> [1] -> null --- [1] -> [2] -> null
Input
head = [2,1], x = 2Output
[1,2]The node with value 1 is less than 2, so it is moved before the node with value 2 while preserving partition order.
Constraints
- The number of nodes in the list is in the range
[0, 200]. -100 <= Node.val <= 100-200 <= x <= 200