#K66192. Reorder Linked List

    ID: 32366 Type: Default 1000ms 256MiB

Reorder Linked List

Reorder Linked List

You are given a singly linked list consisting of n nodes. Your task is to reorder the list in a specific pattern:

$L_0 \rightarrow L_n \rightarrow L_1 \rightarrow L_{n-1} \rightarrow L_2 \rightarrow L_{n-2} \rightarrow \cdots$

This means that the first node will be followed by the last node, then the second node by the second-to-last node, and so on. The reordering must be done in-place and your program should output the final reordered list as a sequence of node values separated by a single space.

Note: If the list is empty, you should output nothing.

inputFormat

The input is read from standard input and contains two lines:

  • The first line contains a single integer n representing the number of nodes in the list.
  • The second line contains n space-separated integers representing the node values in order.

outputFormat

Output the values of the reordered list in a single line. The values should be separated by a single space. If the list is empty, output nothing.

## sample
5
1 2 3 4 5
1 5 2 4 3