Reverse a linked list
Submissions: 48690   Accuracy:

48.93%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Interview Preparation

Given a linked list of length N. The task is to reverse the list.

Input:
First line of input contains number of testcases T. For each testcase, first line contains length of linked list and next line contains the elements of linked list.

Output:
Reverse the linked list and return head of the modified list.

User Task:
The task is to complete the function reverse() which head reference as the only argument and should return new head after reversing the list.

Constraints:
1 <= T <= 100
1 <= N <= 103

Example:
Input:
1
6
1 2 3 4 5 6

Output:
6 5 4 3 2 1

Explanation:
Testcase 1:
After reversing the list, elements are as 6->5->4->3->2->1.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa
Author: deactjustCalvin


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.