Reverse a Linked List

Easy

Given a linked list, reverse it.

Example:

Input:  1→2→3→4→NULL

Output:  4→3→2→1→NULL

Testing

Input Format

The first line contains an integer ‘T’, denoting the independent number of test cases.

For each test case the input has two lines:

  • An integer ‘n’ denoting the length of the linked list.
  • n space-separated integers denoting the elements of the linked list.

Output Format

For each test case, a line containing ‘n’ space-separated integers denoting elements of the reversed linked list.

Sample Input

3
4
1 2 3 4
3
3 4 5
4
1 0 1 2

Expected Output

4 3 2 1
5 4 3
2 1 0 1

Constraints

1 <= T <= 100
1 <= n <= 104
1 <= element <= 105

Editorial Link: Editorial