#C5736. Reverse a Singly Linked List

    ID: 49418 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

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

A singly linked list is defined as a sequence of nodes where each node contains an integer value and a pointer to the next node (or null if it is the last node). The reversal should be performed in place using pointer manipulation.

Formally, if the original list is \(L = [a_1, a_2, \dots, a_n]\), the reversed list should be \(L' = [a_n, a_{n-1}, \dots, a_1]\).

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

  1. The first line contains an integer n denoting the number of nodes in the linked list.
  2. The second line contains n space-separated integers representing the node values. If n is 0, the list is empty.

outputFormat

Output the values of the reversed linked list to standard output (stdout) in a single line, with each value separated by a space. If the list is empty, output nothing.

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