Reverse a Doubly Linked List
Submissions: 11543   Accuracy:


  Difficulty: Easy   Marks: 2

Given a doubly linked list of n elements. The task is to reverse the doubly linked list.

First line of input contains number of testcases T. For each testcase, first line of input contains number of nodes n, to be inserted into the linked list and next line contains data of n nodes.

There will be two lines of output for each testcase, which contains the elements of linked list. The first line contains the elements from head to tail separated by spaces. The second line contains elements fron tail to head separated by spaces.

User Task:
Your task is to complete the given function reverseDLL(), which takes head reference as argument and should reverse the elements so that the tail becomes the new head and all pointers are correctly pointed. You need to return the new head of the reversed list. The printing and verification is done by the driver code.

1 <= T <= 100
1 <= n <= 103
0 <= value <= 103


3 4 5
75 122 59 196 30 38 36 194

5 4 3
194 36 38 30 196 59 122 75

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

Contributor: Amit Khandelwal
Author: bhawnamait

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

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

to report an issue on this page.