Reverse a doubly circular linked list

The problem is to reverse the given doubly circular linked list.

Examples:
Input:
Doubly circular linked list
Output:
Reverse doubly circular linked list



Algorithm:

insertEnd(head, new_node) 
    Declare last

    if head == NULL then
        new_node->next = new_node->prev = new_node
        head = new_node
        return 
     
    last = head->prev
    new_node->next = head  
    head->prev = new_node
    new_node->prev = last  
    last->next = new_node

reverse(head)
    Initialize new_head = NULL
    Declare last
    
    last = head->prev
    Initialize curr = last, prev
    
    while curr->prev != last
        prev = curr->prev
        insertEnd(&new_head, curr)
        curr = prev
    insertEnd(&new_head, curr)
    
    return new_head

Explanation: The variable head in the parameter list of insertEnd() is pointer to a pointer variable. reverse() traverses the doubly circular linked list starting with head pointer in backward direction and one by one gets the node in the traversal. It inserts those nodes at the end of the list that starts with the new_head pointer with the help of the function insertEnd() and finally returns new_head.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to revesre a
// doubly circular linked list
#include <bits/stdc++.h>
  
using namespace std;
  
// structure of a node of linked list
struct Node {
    int data;
    Node *next, *prev;
};
  
// function to create and return a new node
Node* getNode(int data)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    return newNode;
}
  
// Function to insert at the end
void insertEnd(Node** head, Node* new_node)
{
    // If the list is empty, create a single node
    // circular and doubly list
    if (*head == NULL) {
        new_node->next = new_node->prev = new_node;
        *head = new_node;
        return;
    }
  
    // If list is not empty
  
    /* Find last node */
    Node* last = (*head)->prev;
  
    // Start is going to be next of new_node
    new_node->next = *head;
  
    // Make new node previous of start
    (*head)->prev = new_node;
  
    // Make last preivous of new node
    new_node->prev = last;
  
    // Make new node next of old last
    last->next = new_node;
}
  
// Uitlity function to revesre a
// doubly circular linked list
Node* reverse(Node* head)
{
    if (!head)
        return NULL;
  
    // Initialize a new head pointer
    Node* new_head = NULL;
  
    // get pointer to the the last node
    Node* last = head->prev;
  
    // set 'curr' to last node
    Node *curr = last, *prev;
  
    // traverse list in backward direction
    while (curr->prev != last) {
        prev = curr->prev;
  
        // insert 'curr' at the end of the list
        // starting with the 'new_head' pointer
        insertEnd(&new;_head, curr);
        curr = prev;
    }
    insertEnd(&new;_head, curr);
  
    // head pointer of the reversed list
    return new_head;
}
  
// function to display a doubly circular list in
// forward and backward direction
void display(Node* head)
{
    if (!head)
        return;
  
    Node* temp = head;
  
    cout << "Forward direction: ";
    while (temp->next != head) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << temp->data;
  
    Node* last = head->prev;
    temp = last;
  
    cout << "\nBackward direction: ";
    while (temp->prev != last) {
        cout << temp->data << " ";
        temp = temp->prev;
    }
    cout << temp->data;
}
  
// Driver program to test above
int main()
{
    Node* head = NULL;
  
    insertEnd(&head;, getNode(1));
    insertEnd(&head;, getNode(2));
    insertEnd(&head;, getNode(3));
    insertEnd(&head;, getNode(4));
    insertEnd(&head;, getNode(5));
  
    cout << "Current list:\n";
    display(head);
  
    head = reverse(head);
  
    cout << "\n\nReversed list:\n";
    display(head);
  
    return 0;
}

chevron_right


Output:

Current list:
Forward direction: 1 2 3 4 5
Backward direction: 5 4 3 2 1

Reversed list:
Forward direction: 5 4 3 2 1
Backward direction: 1 2 3 4 5

Time Complexity: O(n).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.