All DSA problems
EasyLinked ListAmazonAppleMicrosoft

Reverse Linked List Logic

Given n integers representing node values of a singly linked list in order, reverse the list and print values space-separated.

**Input**

First line: n

Second line: n integers

Examples

Example 1

Input:

5
1 2 3 4 5

Output: 5 4 3 2 1

Example 2

Input:

2
1 2

Output: 2 1

Constraints

  • 1 ≤ n ≤ 5000

Target: O(n) time · O(1) space