Given the head of a linked list and a value x, partition it such that all nodes with value < x come before nodes with value >= x, preserving original relative order within each partition.
Output: [1,2,2,4,3,5]
Explanation: Nodes < 3: 1→2→2. Nodes ≥ 3: 4→3→5. Joined.
Output: [1,2]
Maintain two dummy-headed lists: one for nodes < x, one for nodes >= x. Traverse the original list, appending each node to the appropriate list. Finally connect the two lists.
Complexity Analysis:
Time complexity: O(n)
Space complexity: O(1) — in-place rewiring