Open In App

JavaScript Program to find Length of Linked List using JavaScript

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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:

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);

Output
Length 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);

Output
Length of the linked list is 4

Time Complexity: O(n)

Space Complexity: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads