Open In App

JavaScript Program to Count Nodes in Doubly Linked List

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

Recursive approach

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

Output
Number of nodes: 4

Time Complexity: O(n)

Space Complexity: O(1)

Iterative approach

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

Output
Number of nodes: 5

Time Complexity: O(n)

Space Complexity: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads