Reverse a circular linked list

05 May, 2022
Given a circular linked list of size n. The problem is to reverse the given circular linked list by changing links between the nodes.Examples: INPUT:  
OUTPUT:  
 
Approach: The approach is same as followed in reversing a singly linked list. Only here we have to make one more adjustment by linking the last node of the reversed list to the first node. 
C++
Java
Python3
C#
Javascript
// C++ implementation to reverse// a circular linked list#include <bits/stdc++.h> using namespace std; // Linked list nodestruct Node {    int data;    Node* next;}; // function to get a new nodeNode* getNode(int data){    // allocate memory for node    Node* newNode = new Node;     // put in the data    newNode->data = data;    newNode->next = NULL;    return newNode;} // Function to reverse the circular linked listvoid reverse(Node** head_ref){    // if list is empty    if (*head_ref == NULL)        return;     // reverse procedure same as reversing a    // singly linked list    Node* prev = NULL;    Node* current = *head_ref;    Node* next;    do {        next = current->next;        current->next = prev;        prev = current;        current = next;    } while (current != (*head_ref));     // adjusting the links so as to make the    // last node point to the first node    (*head_ref)->next = prev;    *head_ref = prev;} // Function to print circular linked listvoid printList(Node* head){    if (head == NULL)        return;     Node* temp = head;    do {        cout << temp->data << " ";        temp = temp->next;    } while (temp != head);} // Driver program to test aboveint main(){    // Create a circular linked list    // 1->2->3->4->1    Node* head = getNode(1);    head->next = getNode(2);    head->next->next = getNode(3);    head->next->next->next = getNode(4);    head->next->next->next->next = head;     cout << "Given circular linked list: ";    printList(head);     reverse(&head);     cout << "\nReversed circular linked list: ";    printList(head);     return 0;}
// Java implementation to reverse// a circular linked listclass GFG{ // Linked list nodestatic class Node{    int data;    Node next;}; // function to get a new nodestatic Node getNode(int data){    // allocate memory for node    Node newNode = new Node();     // put in the data    newNode.data = data;    newNode.next = null;    return newNode;} // Function to reverse the circular linked liststatic Node reverse(Node head_ref){    // if list is empty    if (head_ref == null)        return null;     // reverse procedure same as reversing a    // singly linked list    Node prev = null;    Node current = head_ref;    Node next;    do    {        next = current.next;        current.next = prev;        prev = current;        current = next;    } while (current != (head_ref));     // adjusting the links so as to make the    // last node point to the first node    (head_ref).next = prev;    head_ref = prev;    return head_ref;} // Function to print circular linked liststatic void printList(Node head){    if (head == null)        return;     Node temp = head;    do    {        System.out.print( temp.data + " ");        temp = temp.next;    } while (temp != head);} // Driver codepublic static void main(String args[]){    // Create a circular linked list    // 1.2.3.4.1    Node head = getNode(1);    head.next = getNode(2);    head.next.next = getNode(3);    head.next.next.next = getNode(4);    head.next.next.next.next = head;     System.out.print("Given circular linked list: ");    printList(head);     head = reverse(head);     System.out.print("\nReversed circular linked list: ");    printList(head); }} // This code is contributed by Arnab Kundu
# Python3 implementation to reverse# a circular linked listimport math # Linked list nodeclass Node:    def __init__(self, data):        self.data = data        self.next = None # function to get a new nodedef getNode(data):         # allocate memory for node    newNode = Node(data)     # put in the data    newNode.data = data    newNode.next = None    return newNode # Function to reverse the# circular linked listdef reverse(head_ref):         # if list is empty    if (head_ref == None):        return None     # reverse procedure same as    # reversing a singly linked list    prev = None    current = head_ref         next = current.next    current.next = prev    prev = current    current = next    while (current != head_ref):        next = current.next        current.next = prev        prev = current        current = next     # adjusting the links so as to make the    # last node point to the first node    head_ref.next = prev    head_ref = prev    return head_ref # Function to print circular linked listdef printList(head):    if (head == None):        return     temp = head         print(temp.data, end = " ")    temp = temp.next    while (temp != head):        print(temp.data, end = " ")        temp = temp.next # Driver Codeif __name__=='__main__':         # Create a circular linked list    # 1.2.3.4.1    head = getNode(1)    head.next = getNode(2)    head.next.next = getNode(3)    head.next.next.next = getNode(4)    head.next.next.next.next = head     print("Given circular linked list: ",                                end = "")    printList(head)     head = reverse(head)     print("\nReversed circular linked list: ",                                     end = "")    printList(head) # This code is contributed by Srathore
// C# implementation to reverse// a circular linked listusing System; class GFG{     // Linked list node    public class Node    {        public int data;        public Node next;    };         // function to get a new node    static Node getNode(int data)    {        // allocate memory for node        Node newNode = new Node();             // put in the data        newNode.data = data;        newNode.next = null;        return newNode;    }         // Function to reverse the circular linked list    static Node reverse(Node head_ref)    {        // if list is empty        if (head_ref == null)            return null;             // reverse procedure same as reversing a        // singly linked list        Node prev = null;        Node current = head_ref;        Node next;        do        {            next = current.next;            current.next = prev;            prev = current;            current = next;        } while (current != (head_ref));             // adjusting the links so as to make the        // last node point to the first node        (head_ref).next = prev;        head_ref = prev;        return head_ref;    }         // Function to print circular linked list    static void printList(Node head)    {        if (head == null)            return;             Node temp = head;        do        {            Console.Write( temp.data + " ");            temp = temp.next;        } while (temp != head);    }         // Driver code    public static void Main(String []args)    {        // Create a circular linked list        // 1.2.3.4.1        Node head = getNode(1);        head.next = getNode(2);        head.next.next = getNode(3);        head.next.next.next = getNode(4);        head.next.next.next.next = head;             Console.Write("Given circular linked list: ");        printList(head);             head = reverse(head);             Console.Write("\nReversed circular linked list: ");        printList(head);         }} // This code contributed by Rajput-Ji
<script>       // JavaScript implementation to reverse      // a circular linked list      // Linked list node      class Node {        constructor() {          this.data = 0;          this.next = null;        }      }       // function to get a new node      function getNode(data) {        // allocate memory for node        var newNode = new Node();         // put in the data        newNode.data = data;        newNode.next = null;        return newNode;      }       // Function to reverse the circular linked list      function reverse(head_ref) {        // if list is empty        if (head_ref == null) return null;         // reverse procedure same as reversing a        // singly linked list        var prev = null;        var current = head_ref;        var next;        do {          next = current.next;          current.next = prev;          prev = current;          current = next;        } while (current != head_ref);         // adjusting the links so as to make the        // last node point to the first node        head_ref.next = prev;        head_ref = prev;        return head_ref;      }       // Function to print circular linked list      function printList(head) {        if (head == null) return;         var temp = head;        do {          document.write(temp.data + " ");          temp = temp.next;        } while (temp != head);      }       // Driver code      // Create a circular linked list      // 1.2.3.4.1      var head = getNode(1);      head.next = getNode(2);      head.next.next = getNode(3);      head.next.next.next = getNode(4);      head.next.next.next.next = head;       document.write("Given circular linked list: ");      printList(head);       head = reverse(head);       document.write("<br>Reversed circular linked list: ");      printList(head);       </script>
Output:  
Given circular linked list: 1 2 3 4
Reversed circular linked list: 4 3 2 1
Time Complexity: O(n)This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
andrew1234
Rajput-Ji
sapnasingh4991
Akanksha_Rai
rdtank
surinderdawra388
Linked List
Linked List
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
What is Data Structure: Types, Classifications and Applications
Types of Linked List
Rearrange a given linked list in-place.
Find first node of loop in a linked list
Circular Singly Linked List | Insertion
Add two numbers represented by linked lists | Set 2
Flattening a Linked List
Real-time application of Data Structures
Insert a node at a specific position in a linked list