Reversing a double linked list

In Algorithms, C Tidbits, Data Structures in C/C++ on March 30, 2007 at 5:43 pm

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…
reversing double linked list

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.

please refer http://www.refcode.net/2013/02/reversing-double-linked-list.html
  1. overall, very good articles, keep the good work..

  2. good work

  3. 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

  4. 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 ,

  5. 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:


  6. Hi All,..

    I have tried this and i have made this as simple. here is the code for u guys……

    void reverse1( struct node **q)
    struct node *s,*r,*temp;
    s = r = NULL;
    temp = *q;
    temp = temp->next;
    r->prev = temp;
    r->next = s;
    temp->next = r;
    temp->prev = NULL;
    r = temp;
    *q = r;

  7. struct Node *reversedoublelist(struct Node* node)
    static struct Node *tmpnode = NULL;
    static struct Node *headnode = NULL;
    static struct Node *swpnode = NULL;

    if (NULL == node)
    return node;

    if (node->next)
    m = tmpnode->prev;
    tmpnode->prev = tmpnode->next;
    tmpnode->next = m;
    headnode = node;
    tmpnode = headnode;

    headnode->prev = NULL;
    return headnode;

  8. sorry please replace the structure variable ‘m’ in the above code to swpnode….please check the above code works for everyone

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: