Open In App

Second Largest Number in a linked list in JavaScript

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A linked list is a fundamental data structure where each element, known as a node, is connected to the next one, forming a chain. We are given a linked list and have to find the second-largest element in it.

Using iteration

In this method, we traverse the linked list using the while loop, keeping track of the two largest numbers and printing the second largest number at the end.

Example: The below code implements the iteration to find the second largest element in a linked list.

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

    secondLargest() {
        if (!this.head || !this.head.next) {
            console.log("Finding the second largest number" +
                " is not possible in this linked list.");
            return;
        }

        let max1 = Number.NEGATIVE_INFINITY;
        let max2 = Number.NEGATIVE_INFINITY;

        let curr = this.head;

        while (curr) {
            if (curr.data > max1) {
                max2 = max1;
                max1 = curr.data;
            } else if (curr.data > max2 &&
                curr.data !== max1) {
                max2 = curr.data;
            }
            curr = curr.next;
        }

        if (max2 === Number.NEGATIVE_INFINITY) {
            console.log
            ("All elements in the linked list are equal.");
        }
        else {
            console.log
            (`The second largest number in the linked list is ${max2}`);
        }
    }
}

const linkedList = new LinkedList();
linkedList.addNode(31);
linkedList.addNode(12);
linkedList.addNode(71);
linkedList.addNode(5);

linkedList.secondLargest();

Output
The second largest number in the linked list is 31

Time Complexity: O(n)

Space Complexity: O(1)

Using Recursion

In this method, we recursively traverse the linked list, keeping track of the two largest numbers. It prints the second largest number at the end.

Example: The below code uses recursion to traverse through linked list and find second largest element in it.

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

    secondLargestRecursive(node = this.head) {
        if (!node) {
            return {
                max: Number.NEGATIVE_INFINITY,
                secondMax: Number.NEGATIVE_INFINITY
            };
        }

        const remain =
            this.secondLargestRecursive(node.next);

        if (node.data > remain.max) {
            return {
                max: node.data,
                secondMax: remain.max
            };
        } else if (node.data > remain.secondMax) {
            return {
                max: remain.max,
                secondMax: node.data
            };
        } else {
            return remain;
        }
    }
}

const linkedList = new LinkedList();
linkedList.addNode(97);
linkedList.addNode(98);
linkedList.addNode(132);
linkedList.addNode(100);



const { secondMax } =
    linkedList.secondLargestRecursive();

if (secondMax === Number.NEGATIVE_INFINITY) {
    console.log(
        "Finding the second largest number is not possible in this linked list.");
} else {
    console.log(`The second largest number in the linked list is ${secondMax}`);
}

Output
The second largest number in the linked list is 100

Time Complexity: O(n)

Space Complexity: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads