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: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Input: head = [2,1], x = 2
Output: [1,2]
[0, 200].-100 <= Node.val <= 100-200 <= x <= 200The idea is to first convert the linked list into an array, partition the array into two parts, and then reconstruct the linked list. Although not the most efficient, it provides a clear separation of the partition logic.
x and the other containing elements greater than or equal to x.Using two pointers, we directly manipulate the linked list to partition without extra space. This is achieved by creating two dummy head nodes representing partitions for values less than x and for values greater than or equal to x.
lessHead and greaterHead to handle partitions.x to less and others to greater.less list with the greater list.less list.