Reversing a doubly linked list
Given a doubly linked list. Write a function that accepts a pointer to the head of the list and reverse the list.
The structure of the node in a doubly linked list is as below:
struct Node { int data; Node* prev; // pointer to the previous node in the list Node* next; // pointer to the next node in the list };Solution:
I have earlier written post about reversing a singly linked list iteratively and recursively.
Reversing a doubly linked list is much simpler as compared to reversing a singly linked list. We just need to swap the prev & next pointers in all the nodes of the list and need to make head point to the last node of original list [which will be the first node in the reversed list].
Here is the code
Please let me know your comments / feedback / suggestions
-
4 Comments
- Anonymous says:May 8, 2013 at 11:15 am
thank you! it's great.
Reply - Anonymous says:May 8, 2013 at 11:15 am
thank you! it's great.
Reply - krupa says:September 29, 2014 at 11:37 pm
Its awesome! Thanks a lot
Reply - Sara Mohammed says:July 13, 2016 at 7:35 am
thank you it's easy way
Reply
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.