JavaScript Program to find Length of Linked List using JavaScript
Last Updated :
21 Mar, 2024
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:
Using Iterative Method
In 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)
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