#C7846. Reorder Linked List

    ID: 51762 Type: Default 1000ms 256MiB

Reorder Linked List

Reorder Linked List

You are given a singly linked list represented by a sequence of integers. Your task is to reorder the list such that the ordering becomes: first element, last element, second element, second-to-last element, and so on. More formally, if the list is given by (a_1, a_2, \ldots, a_n), then the reordered list should be (a_1, a_n, a_2, a_{n-1}, \ldots).

For example, given the list: 1 2 3 4 5, the result should be: 1 5 2 4 3.

Note that you must implement the algorithm in-place following the input/output specifications using standard input (stdin) and standard output (stdout).

inputFormat

The first line of input contains an integer (n) representing the number of nodes. The second line contains (n) space-separated integers which represent the node values of the linked list.

outputFormat

Output the reordered linked list as a sequence of space-separated integers on a single line.## sample

5
1 2 3 4 5
1 5 2 4 3