#C14283. Reverse a Singly Linked List

    ID: 43915 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

You are given an integer n and a sequence of n integers. The task is to construct a singly linked list by appending each integer in the given order, then reverse the linked list and output the reversed list.

You must implement a linked list data structure with nodes. Consider a node to have a value and a pointer to the next node. More formally, let \( L = [a_1, a_2, \dots, a_n] \) be the list of integers. After reversing, the list becomes \( L' = [a_n, a_{n-1}, \dots, a_1] \). If the list is empty, output nothing.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains a single integer n \( (0 \leq n \leq 10^5) \), representing the number of nodes in the linked list.
  • The second line contains n space-separated integers which represent the values of the nodes in the order they are appended.

outputFormat

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

## sample
1
5
5

</p>