VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-nodes-circular-linked-list/

⇱ Count nodes in Circular linked list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count nodes in Circular linked list

Last Updated : 31 Aug, 2024

Given a circular linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.

👁 Count-nodes-in-Circular-Linked-List
count nodes in the circular linked list.

Using linear traversal - O(n) time and O(1) space

The idea is to start from the head node and traversing the list until it loops back to the head. Since a circular linked list has no end, the traversal continues until the current node equals the head again. Increment the counter variable during each step of the traversal of nodes. Finally return the count, which represent the count of nodes in circular linked list.


Output
5

Time Complexity: O(n), where n is the length of given linked list.
Auxiliary Space: O(1)

Comment
Article Tags: