We’re preparing your current view and syncing the latest data.
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. Please note that the node indexing is 1-based, so the first node is considered odd, the second node even, and so forth.
The input is given as the head of a singly linked list.
Return the head of the reordered singly linked list where all nodes in odd positions appear before the nodes in even positions.
The number of nodes in the linked list is in the range [0, 10^4]. Node values may be arbitrary.
Example 1
Input
head = [1,2,3,4,5]
Output
[1,3,5,2,4]
Explanation
The odd indexed nodes 1, 3, 5 are grouped first, followed by the even indexed nodes 2 and 4.
Example 2
Input
head = [2,1,3,5,6,4,7]
Output
[2,3,6,7,1,5,4]
Explanation
Nodes at odd positions are 2, 3, 6, and 7. Nodes at even positions are 1, 5, and 4. The list is rearranged accordingly.