JavaScript Program to Find Largest Number in a Linked List
Last Updated :
12 Mar, 2024
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.
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;
}
}
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.
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;
}
}
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)
Similar Reads
JavaScript Program to Find N Largest Elements from a Linked List This JavaScript program is designed to identify the N largest elements from a linked list. A linked list is a linear data structure where elements, known as nodes, are connected via pointers. The program iterates through the linked list to determine the N largest elements it contains. Table of Conte
4 min read
Second Largest Number in a linked list in JavaScript 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 th
3 min read
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
JavaScript Linked List Programs JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program
5 min read
Java Program to Return the Largest Element in a List Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions. Input : List = [5, 3, 234, 114, 154] Output : 234 Input : List = {10, 20, 4} Output : 20Approach 1: Using a ForEach Loop Create L
3 min read
How to Sort a LinkedList in Java? A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations.Sorting the nodes of a Singly Linked list in ascending order:Original ListSorted ListWe can sort the LinkedList by many sorting techniques:Selection SortInsertion sortQuick sortMerge sort Me
13 min read