Signup/Sign In

Python program to create a Circular Linked List of n nodes and display it in reverse order

In this tutorial, we will learn about the algorithm for creating as well as reversing a circular linked list along with its implementation in Python. Here, we need to create a circular linked list, then traverse the list in the reverse order and print the nodes.

What is Circular Linked List?

Circular Linked List is a little more complicated linked data structure. In the circular linked list, we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements point to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required.

As the output, we are required to get the list of reverse circular linked list elements.

Look at the given example to understand the working with input and output.


circular linked list: 4 5 3 6 9 7 8


original circular linked list:4 5 3 6 9 7 8

reversed circular linked list:8 7 9 6 3 5 4


circular linked list: 10 2 56


original circular linked list:10 2 56

reversed circular linked list:56 2 10


Step1: The first element in the array is assumed to be sorted. Take the second element and store it separately in "k". Compare "k" with the first element. If the first element is greater than "k", then "k" is placed in front of the first element.

Step2: Now, the first two elements are sorted. Take the third element and compare it with the elements on its left. Placed it just behind the element smaller than it. If there is no element smaller than it, then place it at the beginning of the array.

Step3: Similarly, place every unsorted element at its correct position.

Step4: The above process goes on until the last element.

Python Program to Create a Circular Linked List of n Nodes and Display it in Reverse Order

#Represents the node of list.    
class Node:    
    def __init__(self,data):  = data;  = None;    
class CreateList:    
    #Declaring head and tail pointer as null.    
    def __init__(self):    
      self.head = Node(None);    
      self.tail = Node(None);  = self.tail;  = self.head;    
    #This function will add the new node at the end of the list.    
    def add(self,data):    
      newNode = Node(data);    
      #Checks if the list is empty.    
      if is None:    
        #If list is empty, both head and tail would point to new node.    
        self.head = newNode;    
        self.tail = newNode;    = self.head;    
        #tail will point to new node.    = newNode;    
        #New node will become new tail.    
        self.tail = newNode;    
        #Since, it is circular linked list tail will point to head.    = self.head;    
    #Displays all the nodes in the list    
    def display(self):    
        current = self.head;    
        if self.head is None:    
          print("List is empty");    
            #Prints each node by incrementing pointer.    
            while( != self.head):    
                current =;    
    #Reverse the order of the nodes present in the list.    
    def reverse(self, current):    
        #Checks if the next node is head, if yes then prints it.    
        if( == self.head):    
        #Recursively calls the reverse function    
class CircularLinkedList:    
    cl = CreateList();    
    #Adds data to the list    
    print("Original List: ");    
    print("\nReversed List: ");    
    #Print reversed list    

Original List: 1 2 3 4 5 6
Reversed List: 6 5 4 3 2 1


We have concluded the reversing circular linked list in this tutorial. Also, the implementation part has been shown to produce the program's desired output.