JavaScript program to count nodes in Circular linked list Last Updated : 10 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A circular linked list is a type of linked list in which the last node is connected to the first node, creating a loop or circular structure. We will explore how to count nodes in a circular linked list. Below are the approaches to count nodes in the Circular linked list: Table of Content Iterative approachRecursive approachIterative approachIn this approach, we iterate over a circular linked list using a while loop. We start from the head node and traverse the list until we reach the head node again. In each iteration, we increment the counter to count the nodes. Once we reached the head node again we counted all the nodes in the circular linked list. Example: The example below shows how to count nodes in a Circular linked list using an iterative approach. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } let head = null; function add(data) { const newNode = new Node(data); if (!head) { head = newNode; newNode.next = head; } else { let current = head; while (current.next != head) { current = current.next; } current.next = newNode; newNode.next = head; } } function countNodesLoop() { let current = head; let count = 0; if (head != null) { do { count++; current = current.next; } while (current != head); } return count; } add(1); add(2); add(3); add(4); console.log(countNodesLoop()); Output4 Time Complexity: O(n) Space Complexity: O(1) Recursive approachIn this approach, define a recursive function. This function starts with the head node and recursively calls itself with the next node in the list. In each recursive call, the function increments a count variable to keep track of the number of nodes visited. This process continues until the base case is reached (base case occurs when the current node becomes same as the head node), at this point the function stops recursing and returns the final count. Example: Below is the demonstration to count nodes in Circular linked list recursive approach. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } let head = null; function add(data) { const newNode = new Node(data); if (!head) { head = newNode; newNode.next = head; } else { let current = head; while (current.next !== head) { current = current.next; } current.next = newNode; newNode.next = head; } } function countNodes(current = head, count = 0) { if (!head) { return count; } if (current.next === head) { return count + 1; } return countNodes(current.next, count + 1); } add(1); add(2); add(3); add(4); add(5); console.log(countNodes()); Output5 Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript Program to find Length of Linked List using JavaScript Y yuvrajghule281 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Count Nodes in Doubly Linked List A Doubly Linked List is a type of linked list where each node contains a reference to the previous and next nodes in the sequence. Counting nodes in a Doubly Linked List involves iterating through the list and incrementing a counter for each node encountered. Below are the approaches to count nodes 3 min read JavaScript Program to find Length of Linked List using JavaScript Given a linked list, the task is to determine its length using various methods. The linked list is a fundamental data structure consisting of nodes connected in a chain, offering efficient data organization. Different approaches, including Iterative and Recursive Methods, enable us to compute the le 3 min read Javascript Program for Rotate Doubly linked list by N nodes Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list.  N = 2Rotated List: Examples: Input : a b c d e N = 2Output : c d e a b Input : a b c d e f g h N = 4Output : e f g h a b c d Ask 4 min read Java Program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3-> 3 min read Java Program to Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below: Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Javascript Program for Clockwise rotation of Linked List Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULLInput: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL 4 min read Like