Count Total Nodes in Circular Linked List

count nodes of a linked list in java

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() {
        Node temp = head;
        do{
            cnt++;
            temp = temp.next;
        }while(temp != head);
        System.out.println("Number of Nodes in the list is "+cnt);
    }
Count nodes in Circular Linked List

Algorithm for writing the function to count Nodes in a Circular Linked List

  • countNodes()
  • Node temp = head
  • do while(temp != head)
    • cnt++
    • temp = temp->next

Code to Count Total Nodes in Circular Linked List in JAVA

Prime Course Trailer

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • 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

Circular Linked List