Problem 125
Reorder List
Medium · 20 pts · Linked List, Two Pointers, Stack
Problem
You are given a linked list L0, L1, L2, ..., Ln. Reorder it to alternate between the front and the back: L0, Ln, L1, Ln-1, L2, Ln-2, and so on, and print the result. This can be done in place in three steps: find the middle with slow and fast pointers, reverse the second half, and then merge the two halves by taking one node from each in turn.
Input
First line: integer N, the number of nodes. Second line: the N node values from head to tail.
Output
Print the node values of the resulting list from head to tail, space-separated, or EMPTY if it has no nodes.
Constraints
1 <= N <= 50000; 1 <= value <= 1000
Example input
4 1 2 3 4
Example output
1 4 2 3
Explanation
For 1 2 3 4, take the first node 1, then the last node 4, then the second node 2, then the second-to-last node 3. The result is 1 4 2 3.