# 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():
return 0
initialize count = 1
while temp is not equal to head:
increment count by 1
move temp to the next node
return count``````

## Algorithm Explanation

1. Check if the circular linked list is empty. If it is, return 0, as there are no nodes to count.
2. Initialize a variable `count` to 1 to account for the first node.
3. Start traversal from the second node (since we have already accounted for the first node).
4. While the current node (`temp`) is not equal to the starting node (`head`), increment the `count` by 1 and move `temp` to the next node.
5. Once the traversal completes and we reach the starting node again, return the `count`.

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

## Comment

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.