# Count Total Nodes in Circular Linked List ## JAVA Program to Count the Number of Nodes in a Circular Linked List

In this page, we’ll take a look at a program to Count Total Nodes in Circular Linked List, a loop is required until the very first element reappears. Circular linked lists have a head and tail. The head marks the beginning of the list pointing towards the first first element whereas the tail is the last element that also points to the first element of the list. A loop iterates through the entire list and increases the value of a counter variable for every passing node.

## Steps to be followed to create a program to count the number of Nodes in a Linked List

To count the number of elements in a circular linked list –

• Intiate a count variable with zero
• Store the value of head in current variable.
• Iterate through the loop increasing the value of count by 1 and changing the value of current to the next node.
• Stop the iteration as soon as the current value equals to the head.
• Print the count variable as the total number of elements in the circular linked list.
```public void countNodes() {
do{
cnt++;
temp = temp.next;
System.out.println("Number of Nodes in the list is "+cnt);
}
``` ## Algorithm for writing the function to count Nodes in a Circular Linked List

• countNodes()
• cnt++
• temp = temp->next

## Code to Count Total Nodes in Circular Linked List in JAVA

Run
```import java.util.*;
public class Main {
public static void main(String[] args) {
Main Obj = new Main();

Obj.print();
Obj.countNodes();
}
public class Node{
int element;
Node next;

public Node(int element) {
this.element = element;
}
}
int cnt;
static Node list1, list2;
public Node tail = null;
public void print() {
System.out.println("Empty List");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.element);
current = current.next;
System.out.println();
}
}
System.out.println("Empty List");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.element);
current = current.next;
System.out.println();
}
}

Node newNode = new Node(element);
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void countNodes() {
do{
cnt++;
temp = temp.next;
System.out.println("Number of Nodes in the list is "+cnt);
}
}```

### Output

```Circular Linked List is
10 20 30 40 50 60
Number of Nodes in the list is 6```
Run
```import java.lang.*;
public class Main {
public static void main(String[] args) {
Main Obj = new Main();

System.out.println("Original List");
Obj.print();

Obj.countNodes();
}
public class Node{
int element;
Node next;
Node prev;

public Node(int element) {
this.element = element;
}
}
int cnt;
static Node list1, list2;
public Node tail = null;
public void print() {
System.out.println("null");
}
else {
do{
System.out.print(" "+ temp.element);
temp = temp.next;
System.out.println();
}
}

Node newNode = new Node(element);
tail = newNode;
}
else {
tail.next = newNode;
newNode.prev=tail;
tail = newNode;
}
}
System.out.println("Empty List");
}
else {
do{
//Prints each node by incrementing pointer.
System.out.print(" "+ current.element);
current = current.next;
System.out.println();
}
}

public void countNodes() {
do{
cnt++;
temp = temp.next;
System.out.println("Number of Nodes in the list is "+cnt);
}

}```

### Output

```Original List
10 20 30 40 50 60
Number of Nodes in the list is 6
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java