30
Mar
Reversing a double linked list
Related Blog Items
- Reversing a circular single linked list using pointers
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Detecting broken links in double linked list
- Circular Doubly Linked List With Out Using Special Head or Tail Node
refer double linked list post for information about double linked list. Here in this post we will be discussing about reversing a double linked list.
Here is the pictorial representation of our algo…

in our routine we will swap our next, prev pointers of nodes to reverse the double linked list… this routine capable of reversing entire list for given any node in the double linked list.
The below routine takes input as any node in double linked list and reverse the entire double linked list and returns same node pointer.
Node *reverselist(Node *head) { Node* n = head; Node *tmp, *tmp2; Node* after = head->next; Node* before = head->prev; //reverse all nodes after n (if any) //now reverse nodes after head while(n && after) { tmp = after->next; tmp2 = after; after->next = n; n->prev = after; after = tmp; n = tmp2; } //make the last node prev ptr points to NULL n->prev = NULL; //search all nodes before n (if any) n = head; //now reverse all nodes before head while(before && n) { tmp = before->prev; tmp2 = before; before->prev = n; n->next = before; before = tmp; n = tmp2; } //make the first node next ptr points to NULL n->next = NULL; return head; }
any comments welcome…
Popularity: 49%
You need to log on to convert this article into PDF
Related Blog Items - Reversing a circular single linked list using pointers
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Detecting broken links in double linked list
- Circular Doubly Linked List With Out Using Special Head or Tail Node
Related Blog Items
- Reversing a circular single linked list using pointers
- Reversing a single linked list using stack
- Reversing single linked list recursively
- Detecting broken links in double linked list
- Circular Doubly Linked List With Out Using Special Head or Tail Node
overall, very good articles, keep the good work..
May 18th, 2007 at 12:42 pm
good work
January 16th, 2008 at 5:31 pm
In case of dounly link list, its pretty eay to reverse it. Simply reverse prev and Next pointer in a node. Fot head, set prev = Null and for Tail, Next = Null
March 31st, 2008 at 3:17 pm
Hello!
Well the information given in your website is proved to be very useful for me. I could not find this information in any other website. Specially the reversing of linked lists.
Thank You ,
Kanza
May 1st, 2008 at 12:21 pm
Nice article! I wrote an article on creating a LinkedList class in JavaScript using the MooTools framework. I only wrote a singly-linked list but I may extend it to support doubly-linked lists and in that case, I will certainly put forth the challenge to reverse a doubly-linked list.
Here is the challenge I put forth to reverse a singly-linked list in JS:
http://www.thetruetribe.com/2008/05/javascript-challenge-reverse-linked.html
May 14th, 2008 at 4:15 pm