You get the head of a singly linked list. Number its nodes by position, starting from 1 at the head. Move every node in an odd position (1st, 3rd, 5th, ...) to the front, followed by every node in an even position (2nd, 4th, 6th, ...), and return the head of the new list.
Inside each group, the nodes keep the order they had. The positions decide the groups, not the values stored in the nodes.
Collecting the values into arrays is easy. Can you relink the nodes themselves in one pass with only O(1) extra memory? The judge only checks the answer, so that part is up to you.
The nodes in positions 1, 3 and 5 (5, -2, 0) come first, then those in positions 2, 4 and 6 (8, 6, 3).
The odd positions hold 7, 1 and 2, and the even positions hold the other two 7s.
An empty list stays empty: return null.
Constraints
0 and 10⁴ nodes.-10⁶ <= Node.val <= 10⁶Walk the list once and deal each node onto one of two chains: one for the 1st, 3rd, 5th, ... nodes and one for the 2nd, 4th, 6th, .... Changing next pointers is enough; no new nodes or arrays are needed.
Read the list and put a mark on every other node: the 1st, 3rd, 5th and so on are "odd", the rest are "even". The answer is all the odd nodes in their original order, then all the even ones in theirs. Nothing is sorted or compared. Each node only has to know which of two lines it stands in.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium