Skip to main content

Find the second largest element in a linked list in node js

Js program for Find the second largest element in a linked list. Here problem description and explanation.

// Node JS Program for
// Find second largest number in linked list

// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new node at the end of linked list
	insert(value)
	{
		// Create a new node
		var node = new LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			return;
		}
		var temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(temp.data + " → ");
			// Visit to next node
			temp = temp.next;
		}
		console.log("null");
	}
	// Find second largest node in linked list
	secondLargest()
	{
		if (this.head == null)
		{
			console.log("\nEmpty linked list");
			return;
		}
		// Display given linked list element
		this.display();
		// Define some auxiliary variables
		var big = null;
		var result = null;
		var temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			if (big == null)
			{
				// Get first node
				big = temp;
			}
			else if (big.data < temp.data)
			{
				// Get a new big node
				// Get second largest node
				result = big;
				// Get current largest node
				big = temp;
			}
			else if (big.data != temp.data)
			{
				if (result == null)
				{
					// If in case second largest node empty
					result = temp;
				}
				else if (result.data < temp.data)
				{
					// Get new second largest node
					result = temp;
				}
			}
			// Visit to next node
			temp = temp.next;
		}
		if (result == null)
		{
			// When second largest node are not exist in linked list
			console.log("Second largest element are not exist");
		}
		else
		{
			console.log("Second largest : " + result.data);
		}
	}
}

function main()
{
	// Testing linked list
	var list1 = new SingleLL();
	var list2 = new SingleLL();
	var list3 = new SingleLL();
	var list4 = new SingleLL();
	// Add node in first linked list
	// 6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → null
	list1.insert(6);
	list1.insert(4);
	list1.insert(5);
	list1.insert(10);
	list1.insert(3);
	list1.insert(7);
	list1.insert(9);
	list1.insert(2);
	list1.insert(8);
	list1.secondLargest();
	// Add node in second linked list
	// 1 → 1 → 4 → 2 → 3 → 1 → null
	list2.insert(1);
	list2.insert(1);
	list2.insert(4);
	list2.insert(2);
	list2.insert(3);
	list2.insert(1);
	list2.secondLargest();
	// Add node in third linked list
	// 2 → 6 → 8 → 4 → 8 → 3 → 3 → 2 → null
	list3.insert(2);
	list3.insert(6);
	list3.insert(8);
	list3.insert(4);
	list3.insert(8);
	list3.insert(3);
	list3.insert(3);
	list3.insert(2);
	list3.secondLargest();
	// Add node in fourth linked list
	list4.insert(2);
	list4.insert(2);
	list4.insert(2);
	// 2 → 2 → 2 → null
	list4.secondLargest();
}
// Start program execution
main();

Output

6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → null
Second largest : 9
1 → 1 → 4 → 2 → 3 → 1 → null
Second largest : 3
2 → 6 → 8 → 4 → 8 → 3 → 3 → 2 → null
Second largest : 6
2 → 2 → 2 → null
Second largest element are not exist




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment