Given the head of a singly linked list, group all odd-indexed nodes together followed by the even-indexed nodes. The first node is odd (index 1), second even (index 2), etc.
Output: [1,3,5,2,4]
Explanation: Odd nodes: 1→3→5, even nodes: 2→4. Join them.
Output: [2,3,6,7,1,5,4]
Use two pointers:
- Save evenHead = head.next (the start of even list)
- Weave odd and even pointers alternately
- Connect odd tail to evenHead
Complexity Analysis:
Time complexity: O(n) — single pass
Space complexity: O(1) — in-place, no extra space