#K73202. Reverse a Singly Linked List

    ID: 33923 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

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

The linked list is defined by its nodes, where each node contains an integer data and a pointer to the next node. The reversal of the list should be done in-place with an iterative approach.

Formally, if the linked list is represented as \( L: a_1 \rightarrow a_2 \rightarrow \cdots \rightarrow a_n \) then after reversal, the list should become \( L': a_n \rightarrow a_{n-1} \rightarrow \cdots \rightarrow a_1 \).

Note: If the list is empty, simply output an empty line.

inputFormat

The input is read from stdin and consists of:

  1. An integer N on the first line, which represents the number of nodes in the linked list.
  2. If N > 0, the second line contains N space-separated integers representing the node values in order.

If N is 0, no additional input is provided and the list is considered empty.

outputFormat

The output should be written to stdout as a single line of the reversed linked list elements, separated by a single space. If the list is empty, output an empty line.

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