Count nodes in circular linked list
A circular linked list is a type of linked list in which the last node points back to the first node, creating a closed loop. Each node in the list contains data and a reference (or "next" pointer) to the next node in the sequence. In a circular linked list, the last node's "next" pointer points to the first node, completing the loop.
Problem Statement
The problem is to count the number of nodes in a given circular linked list.
Example
Let's consider a circular linked list with the following nodes: 1 > 3 > 5 > 7 > 9 > 11 > 1
In this example, there are 6 nodes in the circular linked list. The goal is to write a program that can automatically count the number of nodes in any given circular linked list.
Idea to Solve the Problem
To count the number of nodes in a circular linked list, we can start from the first node and traverse the entire list while keeping track of the count. Since the last node points back to the first node, we need to stop the traversal when we reach the starting node again.
Pseudocode
countNode():
if head is null:
return 0
initialize count = 1
initialize temp = head.next
while temp is not equal to head:
increment count by 1
move temp to the next node
return count
Algorithm Explanation
 Check if the circular linked list is empty. If it is, return 0, as there are no nodes to count.
 Initialize a variable
count
to 1 to account for the first node.  Start traversal from the second node (since we have already accounted for the first node).
 While the current node (
temp
) is not equal to the starting node (head
), increment thecount
by 1 and movetemp
to the next node.  Once the traversal completes and we reach the starting node again, return the
count
.
Code Solution

1) Find length of circular linked list in java
2) Find length of circular linked list in c++
3) Find length of circular linked list in c
4) Find length of circular linked list in kotlin
5) Find length of circular linked list in scala
6) Find length of circular linked list in swift
7) Find length of circular linked list in ruby
8) Find length of circular linked list in python
9) Find length of circular linked list in typescript
10) Find length of circular linked list in node js
11) Find length of circular linked list in php
12) Find length of circular linked list in vb.net
13) Find length of circular linked list in c#
14) Find length of circular linked list in golang
Resultant Output Explanation
In the given code, the circular linked list is populated with nodes containing values 1, 3, 5, 7, 9, and 11. The
countNode()
function is called to count the number of nodes in the circular linked list, which returns
6.
Time Complexity
The time complexity of this algorithm is O(n), where n is the number of nodes in the circular linked list. This is because we traverse the entire circular linked list once to count the nodes.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment