Given the head of a singly linked list, fold it onto itself in place: the new order is the first node, then the last, then the second, then the second-to-last, and so on, alternately taking the next unused node from the front and from the back until every node has been used once. Return the head. Move the nodes themselves; do not just rewrite the values. One test checks this: original_positions(values) builds a list from values, runs your fold_list and returns, for each node of the result, the 0-based position that node had in the original list, so original_positions([3, 9, 4, 7]) must be [0, 3, 1, 2].
Examples
Input: head = 3 -> 9 -> 4 -> 7
Output: 3 -> 7 -> 9 -> 4
Input: head = 12 -> 5 -> 8 -> 1 -> 6
Output: 12 -> 6 -> 5 -> 1 -> 8
Constraints
0 <= number of nodes <= 10**4- Target: O(n) time, O(1) extra space
Goals
- Combine splitting, reversing and interleaving into one algorithm
- Keep every operation in place with O(1) extra space