All problems

Problem 382

Odd Even Linked List

Medium · 20 pts · Linked List

Problem

Group the nodes of a linked list by position: first all the nodes at odd positions (the 1st, 3rd, 5th, ...), then all the nodes at even positions (the 2nd, 4th, ...). Positions are counted from 1 and have nothing to do with the values stored in the nodes. Keep the original relative order inside each group. The intended solution relinks the nodes in place with O(1) extra memory.

Input

The first line contains N, the number of nodes. The second line contains the N node values from head to tail.

Output

Print the node values of the resulting list from head to tail, separated by spaces, or EMPTY if the list has no nodes.

Constraints

0 <= N <= 10000; -1000000 <= value <= 1000000

Example input

5
1 2 3 4 5

Example output

1 3 5 2 4

Explanation

In 1 2 3 4 5 the nodes at odd positions hold 1, 3, 5 and those at even positions hold 2, 4. Joining the two groups gives 1 3 5 2 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Delete the Middle Node Next: Sort a Linked List