reverse doubly linked list

void reverse(struct node **head_ref)
{
     struct node *temp = NULL; 
     struct node *current = *head_ref;
      
     /* swap next and prev for all nodes of
       doubly linked list */
     while (current !=  NULL)
     {
       temp = current->prev;
       current->prev = current->next;
       current->next = temp;             
       current = current->prev;
     }     
      
     /* Before changing head, check for the cases like empty
        list and list with only one node */
     if(temp != NULL )
        *head_ref = temp->prev;
}    
 
 

Comments

Post a Comment

Popular posts from this blog

How whatsapp works

Android N Developer Preview? It's exciting for most of you