JavaScript Program to find Length of Linked List using JavaScript Last Updated : 21 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 length of a linked list easily. Use the below approaches to find the length of the Linked List: Table of Content Using Iterative MethodUsing the Recursive Method Using Iterative MethodIn this approach, a 'Node' class represents a single element in a linked list, with each node storing data and a reference to the next node. The 'LinkedList' class manages the list, allowing the addition of nodes and calculation of its length by traversing from the head to the end. Example: Implementation of Finding the Length of a Linked List using the Iterative Method. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } addNode(data) { const newNode = new Node(data); if (!this.head) this.head = newNode; else { let curr = this.head; while (curr.next) curr = curr.next; curr.next = newNode; } } findLength() { let length = 0; let current = this.head; while (current) { length++; current = current.next; } return length; } } // Example const linkedList = new LinkedList(); linkedList.addNode(3); linkedList.addNode(1); linkedList.addNode(4); linkedList.addNode(2); const length = linkedList.findLength(); console.log("Length of the linked list is", length); OutputLength of the linked list is 4 Time Complexity: O(n) Space Complexity: O(1) Using the Recursive Method In this approach, the "findLength" method checks if the node is null, it returns 0. Otherwise, it recursively calls itself with the next node, adding 1 to the result each time until it reaches the end of the list. This counting process repeats until we have looked at all nodes, giving us the total length of the linked list. Example: Implementation of Finding the Length of a Linked List using Recursive Method. JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } addNode(data) { const newNode = new Node(data); if (!this.head) this.head = newNode; else { let curr = this.head; while (curr.next) curr = curr.next; curr.next = newNode; } } findLength(node = this.head) { if (!node) return 0; return 1 + this.findLength(node.next); } } // Example const linkedList = new LinkedList(); linkedList.addNode(7); linkedList.addNode(5); linkedList.addNode(4); linkedList.addNode(3); const length = linkedList.findLength(); console.log("Length of the linked list is", length); OutputLength of the linked list is 4 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 A abhinavsrivastava1920 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Find Largest Number in a Linked List Finding the largest number in a linked list is a common problem and can be efficiently solved using both iterative and recursive approaches. A linked list is a fundamental data structure where each element, known as a node, is connected to the next one, forming a chain. Table of Content Iterative Ap 3 min read JavaScript Program for Implementation of Stack using Linked List A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, where elements are added and removed from the same end, known as the top of the stack. Here we'll understand how to implement a stack data structure using a linked list and provide operations such as push, pop, 2 min read JavaScript Program to Find Sum of Elements in Linked List We are given a linked list, we have to find the sum of all the elements of that linked list. A linked list is a data structure to store data where each piece of information is connected to the next one, forming a chain. Each piece has its data and knows where to find the next piece. It's a basic but 4 min read JavaScript Program to Find N Largest Elements from a Linked List This JavaScript program is designed to identify the N largest elements from a linked list. A linked list is a linear data structure where elements, known as nodes, are connected via pointers. The program iterates through the linked list to determine the N largest elements it contains. Table of Conte 4 min read 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 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 Remove Loop in a Linked List A linked list is a collection of nodes that contain both a data element and a reference (or pointer) to the next node in the collection. A common concern with linked lists is having loops and cycles. When one of the nodes in the linked list points towards any of its previous nodes leading to an infi 7 min read JavaScript Program to Reverse a Linked List A linked list is a data structure where elements are stored in nodes, and each node points to the next node in the sequence. Reversing a linked list involves changing the direction of the pointers, making the last node the new head and the head the new tail. Below are the approaches to reverse a lin 3 min read JavaScript Program for Sum of Number Digits in a Linked List We are going to write a JavaScript program on how to find the sum of number digits stored in a linked list. We are going to use the Iterative and Recursive techniques to find the Sum of number digits. A linked list is a data structure where elements are stored in nodes and each node points to the ne 2 min read Delete Operations in Doubly Linked List using JavaScript This article will demonstrate how to perform delete operations in a Doubly Linked List Data Structure using JavaScript. We will see different approaches to deleting elements from the Doubly Linked List. Steps to Delete Element Node from Doubly Linked ListA doubly linked list has two pointers that li 11 min read Like