#C7603. Rearrange Linked List: Odd-Even Separation

    ID: 51493 Type: Default 1000ms 256MiB

Rearrange Linked List: Odd-Even Separation

Rearrange Linked List: Odd-Even Separation

Given a singly linked list, your task is to rearrange the nodes so that all the nodes at odd positions come first, followed by all the nodes at even positions. Positions are 1-indexed (i.e., the first element is at position 1, the second at position 2, etc.).

Formally, if the linked list is given as (L = {a_1, a_2, \ldots, a_n}), then the list should be rearranged to ({a_1, a_3, a_5, \ldots, a_2, a_4, a_6, \ldots}).

Example: If the input list is [1, 2, 3, 4, 5], the output should be [1, 3, 5, 2, 4].

inputFormat

The input consists of two lines:

  1. The first line contains a single integer (n) ((1 \le n \le 10^5)), representing the number of nodes in the linked list.
  2. The second line contains (n) space-separated integers representing the node values.

outputFormat

Output a single line containing the rearranged list of node values separated by spaces. The odd-indexed values (1-based) should appear first followed by the even-indexed values.## sample

5
1 2 3 4 5
1 3 5 2 4