Count nodes in Circular linked list
Given a circular linked list, count number of nodes in it. For example, the output is 5 for below list.

We use the concept used in Circular Linked List | Set 2 (Traversal). While traversing, we keep track of count of nodes.
C++
// C program to count number of nodes in // a circular linked list. #include <stdio.h> #include <stdlib.h> /* structure for a node */struct Node { int data; struct Node* next; }; /* Function to insert a node at the beginning of a Circular linked list */void push(struct Node** head_ref, int data) { struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node)); struct Node* temp = *head_ref; ptr1->data = data; ptr1->next = *head_ref; /* If the linked list is not NULL then set the next of last node */ if (*head_ref != NULL) { while (temp->next != *head_ref) temp = temp->next; temp->next = ptr1; } else ptr1->next = ptr1; /*For the first node */ *head_ref = ptr1; } /* Function to print nodes in a given Circular linked list */int countNodes(struct Node* head) { struct Node* temp = head; int result = 0; if (head != NULL) { do { temp = temp->next; result++; } while (temp != head); } return result; } /* Driver program to test above functions */int main() { /* Initialize lists as empty */ struct Node* head = NULL; push(&head;, 12); push(&head;, 56); push(&head;, 2); push(&head;, 11); printf("%d", countNodes(head)); return 0; } |
chevron_right
filter_none
Java
// Java program to count number of nodes in // a circular linked list. class GFG { /* ure for a node */static class Node { int data; Node next; }; /* Function to insert a node at the beginning of a Circular linked list */static Node push( Node head_ref, int data) { Node ptr1 = new Node(); Node temp = head_ref; ptr1.data = data; ptr1.next = head_ref; /* If linked list is not null then set the next of last node */ if (head_ref != null) { while (temp.next != head_ref) temp = temp.next; temp.next = ptr1; } else ptr1.next = ptr1; /*For the first node */ head_ref = ptr1; return head_ref; } /* Function to print nodes in a given Circular linked list */static int countNodes( Node head) { Node temp = head; int result = 0; if (head != null) { do { temp = temp.next; result++; } while (temp != head); } return result; } /* Driver program to test above functions */public static void main(String args[]) { /* Initialize lists as empty */ Node head = null; head = push(head, 12); head = push(head, 56); head = push(head, 2); head = push(head, 11); System.out.printf("%d", countNodes(head)); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
C#
// C# program to count number of nodes in // a circular linked list. using System; class GFG { /* structure for a node */public class Node { public int data; public Node next; }; /* Function to insert a node at the beginning of a Circular linked list */static Node push( Node head_ref, int data) { Node ptr1 = new Node(); Node temp = head_ref; ptr1.data = data; ptr1.next = head_ref; /* If linked list is not null then set the next of last node */ if (head_ref != null) { while (temp.next != head_ref) temp = temp.next; temp.next = ptr1; } else ptr1.next = ptr1; /*For the first node */ head_ref = ptr1; return head_ref; } /* Function to print nodes in a given Circular linked list */static int countNodes( Node head) { Node temp = head; int result = 0; if (head != null) { do { temp = temp.next; result++; } while (temp != head); } return result; } /* Driver code */public static void Main(String []args) { /* Initialize lists as empty */ Node head = null; head = push(head, 12); head = push(head, 56); head = push(head, 2); head = push(head, 11); Console.Write( countNodes(head)); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
4
This article is contributed by Rishabh jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Sum of the nodes of a Circular Linked List
- Delete all the even nodes of a Circular Linked List
- Exchange first and last nodes in Circular Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Count of Prime Nodes of a Singly Linked List
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert singly linked list into circular linked list
- Check if a linked list is Circular Linked List
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Deletion from a Circular Linked List
- Reverse a circular linked list
- Circular Linked List | Set 2 (Traversal)



