Reverse Linked List

EasyLinked List
Next
Linked ListPointer Reversal

Reverse a singly linked list and print the values of the reversed list. The values are given as an array; build the linked list from them yourself, then solve it with pointers.

Input

Line 1: n, the number of nodes. Line 2: the n node values from head to tail.

Output

Print the reversed list's values on one line, from the new head to the new tail.

Constraints

1 <= n <= 5000 -10^4 <= value <= 10^4

Example 1
Input
5
1 2 3 4 5
Output
5 4 3 2 1

The nodes come out in the opposite order.

Hints

Reverse Linked List - Easy Linked List Problem | Skill Shiksha