Open In App

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.

Iterative Approach to Find the Largest Number in a Linked List

In the iterative approach, we traverse the linked list step by step using a while loop. While going through each node, we compare the current node's data with the current maximum value.

If the current node's data is greater than the current maximum, we update the maximum. This process continues until we reach the end of the linked list.

If the linked list is empty, we print an appropriate message indicating that the linked list is empty.

Example: This example shows the use of the above-mentioned approach.

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

    largestNumber() {
        // This is condition to check that linked list is empty or not
        if (!this.head) {
            console.log("Linked list is empty.");
            return;
        }

        let current = this.head;
        let largest = current.data;

        while (current) {
            if (current.data > largest) {
                largest = current.data;
            }
            current = current.next;
        }

        console.log(
            `The largest number in the linked list is ${largest}`);
    }
}

const linkedList = new LinkedList();
linkedList.addNode(3);
linkedList.addNode(12);
linkedList.addNode(7);
linkedList.addNode(5);

linkedList.largestNumber();

Output:

The largest number in the linked list is 12

Time Complexity: O(n)

Space Complexity: O(1)

Recursive Approach to find the Largest Number in a Linked List

In the recursive approach, we solve the problem by looking at one node at a time. We begin by checking the current node and then repeat the process with the remaining nodes in the list.

If we find a larger number during this process, we update the maximum value according to that number.

If the linked list is empty, we print an appropriate message indicating that the linked list is empty.

Example: This example shows the use of the above-mentioned approach.

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

    largestNumber(node = this.head) {
        if (!node) {
        
        // It will return a default value for an empty list
            return Number.NEGATIVE_INFINITY; 
        }

        const remaining = this.largestNumber(node.next);
        return Math.max(node.data, remaining);
    }
}

const linkedList = new LinkedList();
linkedList.addNode(8);
linkedList.addNode(15);
linkedList.addNode(4);
linkedList.addNode(11);

const largest = linkedList.largestNumber();

if (largest !== Number.NEGATIVE_INFINITY) {
    console.log(
        `The largest number in the linked list is ${largest}`);
} else {
    console.log("The linked list is empty.");
}

Output:

The largest number in the linked list is 15

Time Complexity: O(n)

Space Complexity: O(n)

Article Tags :