#C9359. Reverse a Linked List Iteratively

    ID: 53443 Type: Default 1000ms 256MiB

Reverse a Linked List Iteratively

Reverse a Linked List Iteratively

You are given a singly linked list. Your task is to reverse the linked list iteratively and output the reversed list.

The linked list is represented by a sequence of integers. For example, if the input is 1 2 3 4 5 (representing the list 1→2→3→4→5), then after reversing, you should output 5 4 3 2 1.

Note: Your solution should work for an empty list as well as lists with a single element.

The reversal must be done by iteratively modifying the next pointers of the nodes.

You can use the following LaTeX formula to denote the reversal process:

\( head = \{a_1, a_2, \ldots, a_n\} \quad \rightarrow \quad head_{reversed} = \{a_n, a_{n-1}, \ldots, a_1\} \)

inputFormat

The first line contains a single integer n (0 ≤ n ≤ 105), the number of nodes in the linked list.

The second line contains n space-separated integers representing the node values of the linked list.

If n is 0, the list is empty.

outputFormat

Output the values of the reversed linked list in a single line, separated by spaces. If the list is empty, output nothing.

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