Dark Mode On/Off

# 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.

Input:

circular linked list : 3 5 1 2 6

Output:

5

Input:

circular linked list : 73 5 63 9 2 10 3

Output:

7

## Algorithm

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() .

Step3:

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.

Step4:

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):
self.data = data;
self.next = None;

class CreateList:
#Declaring head and tail pointer as null.
def __init__(self):
self.count = 0;
self.tail = Node(None);

#This function will add the new node at the end of the list.
newNode = Node(data);
#Checks if the list is empty.
#If list is empty, both head and tail would point to new node.
self.tail = newNode;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;

#This function will count the nodes of circular linked list
def countNodes(self):
self.count=self.count+1;
self.count=self.count+1;
current = current.next;
print("Count of nodes present in circular linked list: "),
print(self.count);

cl = CreateList();