Second Largest Number in a linked list in JavaScript Last Updated : 13 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like 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. Table of Content Using iterationUsing RecursionUsing iterationIn 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(); OutputThe second largest number in the linked list is 31 Time Complexity: O(n) Space Complexity: O(1) Using RecursionIn 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}`); } OutputThe second largest number in the linked list is 100 Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article JavaScript Program to Find Smallest Number in a Linked List A abhinavsrivastava1920 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads JavaScript Program to Find Smallest Number in a Linked List Given a linked list, the task is to find the smallest number in a linked list in JavaScript. 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 b 3 min read Merge Sort for Linked Lists in JavaScript Prerequisite: Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. In this post, Merge sort for lin 4 min read Javascript Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Below are the steps : Reverse given linked list. For example, 1-> 5 min read Insert Operation in Doubly Linked List using JavaScript This article demonstrates the insert operation in a Doubly Linked List using JavaScript. The insertion in the Doubly linked list follows the given Steps. Steps to Insert Element in Doubly Linked ListCreate a new Node element with the input data/ value.Link the new node previous to the current elemen 6 min read Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node 7 min read Javascript Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list.Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n = 10 min read Like