Odd Even Linked List
MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.You get the head of a linked list. Regroup it so that all nodes at odd positions (1st, 3rd, 5th, ...) come first, followed by all nodes at even positions (2nd, 4th, ...). Positions are about where a node sits, not its value. Keep the relative order inside each group, and do it in O(n) time with O(1) extra space.
1 -> 2 -> 3 -> 4 -> 5 → 1 -> 3 -> 5 -> 2 -> 4
2 -> 1 -> 3 -> 5 -> 6 -> 4 -> 7 → 2 -> 3 -> 6 -> 7 -> 1 -> 5 -> 4Constraints
0 <= number of nodes <= 10^4-10^6 <= Node.val <= 10^6