Find the second largest element in a linked list in java
Java program for Find the second largest element in a linked list. Here problem description and other solutions.
// Java Program for
// Find second largest number in linked list
// Linked list node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
// Set head and tail
this.head = null;
this.tail = null;
}
// Add new node at the end of linked list
public void insert(int value)
{
// Create a new node
LinkNode node = new LinkNode(value);
if (this.head == null)
{
this.head = node;
}
else
{
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
return;
}
LinkNode temp = this.head;
// iterating linked list elements
while (temp != null)
{
System.out.print(temp.data + " → ");
// Visit to next node
temp = temp.next;
}
System.out.println("null");
}
// Find second largest node in linked list
public void secondLargest()
{
if (this.head == null)
{
System.out.println("\nEmpty linked list");
return;
}
// Display given linked list element
this.display();
// Define some auxiliary variables
LinkNode big = null;
LinkNode result = null;
LinkNode 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
System.out.println("Second largest element are not exist");
}
else
{
System.out.println("Second largest : " + result.data);
}
}
public static void main(String[] args)
{
// Testing linked list
SingleLL list1 = new SingleLL();
SingleLL list2 = new SingleLL();
SingleLL list3 = new SingleLL();
SingleLL 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();
}
}
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
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