Partition List
Problem statement
Given the head of a singly linked list and an integer x, reorder the existing nodes so that every node with value less than x appears before every node with value greater than or equal to x.
Preserve the original relative order inside both partitions and return the reordered head.
Function
partitionList(head: ListNode, x: int) → ListNodeExamples
Example 1
head = [1,4,3,2,5,2]x = 3return = [1,2,2,4,3,5]The nodes below 3 keep order 1,2,2; the remaining nodes keep order 4,3,5.
Example 2
head = [2,1]x = 2return = [1,2]The node 1 moves into the less-than partition while relative order remains stable.
Example 3
head = []x = 0return = []An empty list remains empty.
Constraints
- The list contains at most
200nodes. -100 <= Node.val <= 100.-100 <= x <= 100.