Group all nodes at odd positions (1st, 3rd, 5th, ...) first, followed by all nodes at even positions (2nd, 4th, ...). Keep the original order inside each group. Use O(1) extra memory and re-use the nodes.
For 1 -> 2 -> 3 -> 4 -> 5 the answer is 1 -> 3 -> 5 -> 2 -> 4. The position counts, not the value.
The tests show a list as a Python list of its values: [1, 2, 3] stands for 1 -> 2 -> 3.
Example 1
Input:
odd_even_list(head) # head = 1 -> 2 -> 3 -> 4 -> 5
Output:
[1, 3, 5, 2, 4]
Example 2
Input:
odd_even_list(head) # head = 2 -> 1 -> 3 -> 5 -> 6 -> 4 -> 7
Output:
[2, 3, 6, 7, 1, 5, 4]
0 <= number of nodes <= 100000-10**9 <= val <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.