JavaScript Program to Count Nodes in Doubly Linked List Last Updated : 08 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 in doubly linked list: Table of Content Recursive approachIterative approachRecursive approachIn the recursive approach, we define a function that counts nodes recursively. We start by checking if the current node is null. It means we have reached the end of the list. If the current node is not null we will increase the count by 1 and then call the function recursively with the next node in the list. This process continues till we reach the final node. Example: The example below shows the demonstration to count nodes in Doubly linked list using Recursive approach. JavaScript class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } addNode(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } } const dll = new DoublyLinkedList(); dll.addNode(1); dll.addNode(2); dll.addNode(3); dll.addNode(4); function countNodes(head) { let count = 0; let current = head; while (current !== null) { count++; current = current.next; } return count; } console.log("Number of nodes:", countNodes(dll.head)); OutputNumber of nodes: 4 Time Complexity: O(n) Space Complexity: O(1) Iterative approachIn the iterative approach to counting nodes in a Doubly Linked List, we start by setting a count variable to 0. Then, we begin traversing the list from the head. Using a while loop, we keep moving to the next node in the list and incrementing the count for each node we visit. This traversal continues until we reach the end of the list. Example: The example below shows the demonstration to count nodes in Doubly linked list using Iterative approach. JavaScript class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } add(data) { const newNode = new Node(data); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } } function countRecursive(node) { if (node === null) { return 0; } return 1 + countRecursive(node.next); } const dll = new DoublyLinkedList(); dll.add(7); dll.add(5); dll.add(4); dll.add(9); dll.add(10); console.log("Number of nodes:", countRecursive(dll.head)); OutputNumber of nodes: 5 Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article Javascript Program For Sorting A Linked List Of 0s, 1s And 2s Y yuvraj07 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript program to count nodes in Circular linked list 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 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 Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read Javascript Program For Counting Rotations In Sorted And Rotated Linked List Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k.The idea is to traverse singly linked list to check condition whether current node value is greater than value of next node. If the given condition is true, then break the loop. Otherwise increase t 3 min read Javascript Program For Sorting A Linked List Of 0s, 1s And 2s Given a linked list of 0s, 1s and 2s, sort it.Examples:Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULLOutput: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULLInput: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULLSource 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 Like