#K65017. Odd-Even Linked List Rearrangement
Odd-Even Linked List Rearrangement
Odd-Even Linked List Rearrangement
You are given a singly linked list with ( n ) nodes. Your task is to rearrange the list so that all nodes at odd indices are placed first, followed by all nodes at even indices. The relative order among odd-indexed nodes and even-indexed nodes must be maintained. More formally, for a linked list ( L_1 \to L_2 \to \dots \to L_n ), you need to rearrange it into ( L_1 \to L_3 \to L_5 \to \dots \to L_2 \to L_4 \to \dots ). If the list is empty or contains only one node, it should be returned as is.
inputFormat
Input is read from standard input. The first line contains an integer ( n ) representing the number of nodes in the linked list. The second line contains ( n ) space-separated integers representing the node values. When ( n = 0 ), the linked list is considered empty.
outputFormat
Output the rearranged linked list as a sequence of space-separated integers on a single line to standard output.## sample
5
1 2 3 4 5
1 3 5 2 4