Find the largest and second largest value in a Linked List
Given a Linked List, the task is to find the largest and second largest value in a Linked List.
Example:
Input: LL = 10 -> 15 -> 5 -> 20 -> 7 -> 9
Output:
Largest = 20
Second Largest = 15
Input: LL = 0 -> 5 -> 52 -> 21
Output:
Largest = 52
Second Largest = 21
Approach:
- Store the maximum of first two nodes in a variable max.
- Store the minimum of first two nodes in a variable second_max.
- Iterate over the remaining linked list. For each node:
- If current node value is greater than max, then set second_max as max and max as current node’s value.
- Else if current node value is greater than second_max, then set second_max as current node’s value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node
= ( struct Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void findLargestAndSecondLargest( struct Node* head)
{
int val1 = head->data,
val2 = head->next->data,
max = std::max(val1, val2),
second_max = std::min(val1, val2);
head = head->next->next;
while (head != NULL) {
if (head->data > max) {
second_max = max;
max = head->data;
}
else if (head->data > second_max) {
second_max = head->data;
}
head = head->next;
}
cout << "Largest = "
<< max << endl;
cout << "Second Largest = "
<< second_max << endl;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 5);
push(&head, 15);
push(&head, 10);
push(&head, 7);
push(&head, 6);
push(&head, 11);
push(&head, 9);
findLargestAndSecondLargest(head);
return 0;
}
|
Java
class GFG{
static class Node {
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node
= new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void findLargestAndSecondLargest(Node head)
{
int val1 = head.data,
val2 = head.next.data,
max = Math.max(val1, val2),
second_max = Math.min(val1, val2);
head = head.next.next;
while (head != null ) {
if (head.data > max) {
second_max = max;
max = head.data;
}
else if (head.data > second_max) {
second_max = head.data;
}
head = head.next;
}
System.out.print( "Largest = "
+ max + "\n" );
System.out.print( "Second Largest = "
+ second_max + "\n" );
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 20 );
head = push(head, 5 );
head = push(head, 15 );
head = push(head, 10 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 11 );
head = push(head, 9 );
findLargestAndSecondLargest(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
def findLargestAndSecondLargest( self ):
Head = self .head
val1 = Head.data
val2 = Head. next .data
Max = max (val1, val2)
second_max = min (val1, val2)
Head = Head. next . next
while (Head ! = None ):
if (Head.data > Max ):
second_max = Max
Max = Head.data
elif (Head.data > second_max):
second_max = Head.data
Head = Head. next
print ( "Largest = " , Max )
print ( "Second Largest = " , second_max)
if __name__ = = '__main__' :
head = LinkedList()
head.push( 20 )
head.push( 5 )
head.push( 15 )
head.push( 10 )
head.push( 7 )
head.push( 6 )
head.push( 11 )
head.push( 9 )
head.findLargestAndSecondLargest()
|
C#
using System;
class GFG{
class Node {
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node
= new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void findLargestAndSecondLargest(Node head)
{
int val1 = head.data,
val2 = head.next.data,
max = Math.Max(val1, val2),
second_max = Math.Min(val1, val2);
head = head.next.next;
while (head != null ) {
if (head.data > max) {
second_max = max;
max = head.data;
}
else if (head.data > second_max) {
second_max = head.data;
}
head = head.next;
}
Console.Write( "Largest = "
+ max + "\n" );
Console.Write( "Second Largest = "
+ second_max + "\n" );
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 20);
head = push(head, 5);
head = push(head, 15);
head = push(head, 10);
head = push(head, 7);
head = push(head, 6);
head = push(head, 11);
head = push(head, 9);
findLargestAndSecondLargest(head);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function findLargestAndSecondLargest(head)
{
var val1 = head.data,
val2 = head.next.data,
max = Math.max(val1, val2),
second_max = Math.min(val1, val2);
head = head.next.next;
while (head != null ) {
if (head.data > max) {
second_max = max;
max = head.data;
}
else if (head.data > second_max) {
second_max = head.data;
}
head = head.next;
}
document.write( "Largest = "
+ max + "<br>" );
document.write( "Second Largest = "
+ second_max + "<br>" );
}
var head = null ;
head = push(head, 20);
head = push(head, 5);
head = push(head, 15);
head = push(head, 10);
head = push(head, 7);
head = push(head, 6);
head = push(head, 11);
head = push(head, 9);
findLargestAndSecondLargest(head);
</script>
|
Output:
Largest = 20
Second Largest = 15
Performance Analysis:
- Time Complexity: In the above approach, as we are iterating over the linked list only once, so the time complexity is O(N).
- Auxiliary Space Complexity: In the above approach, we are not using any extra space apart from a few constant size variables, so Auxiliary space complexity is O(1).
Last Updated :
18 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...