Signup/Sign In

Python program to create a Circular Linked List of N nodes and count the number of nodes

In this tutorial, we will learn about the algorithm and its implementation for finding the number of nodes in a circular linked list in Python.

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.

Here, we have to find out the number of nodes present in the circular linked list. We first create the circular linked list, then traverse through the list and increment variable 'count' by 1.

As the output, we are required to get the count of nodes in the circular linked list.

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


circular linked list : 3 5 1 2 6




circular linked list : 73 5 63 9 2 10 3




Step1: Define a Node class that represents a node in the list. It has two properties data and next which will point to the next node.

Step2: Define another class for creating the circular linked list and it has two nodes like head and tail. It has two methods: add() and display() .


  1. add() will add the node to the list:
  2. It first checks whether the size is null or the head is null; then it will insert the node as the head.
  3. Both head and tail will point to a newly added node.
  4. If the head is not null, the new node will be the new tail, and the new tail will point to the head as it is a circular linked list.


  1. countNodes() will count the number of nodes present in the list.
  2. Define new node current which will point to the head node.
  3. Traverse through the list to count the nodes by making the current node point to the next node in the list till the current points head again.

Python Program to Create a Circular Linked List of N nodes and count the number of nodes

#Represents the node of the list.  
class Node:  
    def __init__(self,data):  = data;  = None;  
class CreateList:  
    #Declaring head and tail pointer as null.  
    def __init__(self):  
        self.count = 0;  
        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;  
    #This function will count the nodes of circular linked list  
    def countNodes(self):  
        current = self.head;  
        while( != self.head):  
            current =;  
        print("Count of nodes present in circular linked list: "),  
class CircularLinkedList:  
    cl = CreateList();  
    #Adds data to the list  
    #Displays all the nodes present in the list  

Count of nodes present in circular linked list:


We have seen one of the applications of circular linked lists in this tutorial. Also, the implementation part has been shown to produce the program's desired output.