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:



  1. Store the maximum of first two nodes in a variable max.
  2. Store the minimum of first two nodes in a variable second_max.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the largest and
// second largest element in a Linked List
  
#include <bits/stdc++.h>
using namespace std;
  
// Link list node
struct Node {
    int data;
    struct Node* next;
};
  
// Function to push the node at the
// beginning of the linked list
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;
}
  
// Function to print the largest
// and second largest element
void findLargestAndSecondLargest(struct Node* head)
{
    // initialise max and second max using
    // first two nodes of linked list
    int val1 = head->data,
        val2 = head->next->data,
        max = std::max(val1, val2),
        second_max = std::min(val1, val2);
  
    // move the head pointer to 3rd node
    head = head->next->next;
  
    // iterate over rest of linked list
    while (head != NULL) {
  
        if (head->data > max) {
  
            // If current node value is greater
            // than max, then set second_max as
            // current max value and max as
            // current node value
            second_max = max;
            max = head->data;
        }
        else if (head->data > second_max) {
  
            // else if current node value is
            // greater than second_max, set
            // second_max as node value
            second_max = head->data;
        }
  
        // move the head pointer to next node
        head = head->next;
    }
  
    // Print the largest
    // and second largest value
    cout << "Largest = "
         << max << endl;
    cout << "Second Largest = "
         << second_max << endl;
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the largest and
// second largest element in a Linked List
class GFG{
   
// Link list node
static class Node {
    int data;
    Node next;
  
};
   
// Function to push the node at the
// beginning of the linked list
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;
}
   
// Function to print the largest
// and second largest element
static void findLargestAndSecondLargest(Node head)
{
    // initialise max and second max using
    // first two nodes of linked list
    int val1 = head.data,
        val2 = head.next.data,
        max = Math.max(val1, val2),
        second_max = Math.min(val1, val2);
   
    // move the head pointer to 3rd node
    head = head.next.next;
   
    // iterate over rest of linked list
    while (head != null) {
   
        if (head.data > max) {
   
            // If current node value is greater
            // than max, then set second_max as
            // current max value and max as
            // current node value
            second_max = max;
            max = head.data;
        }
        else if (head.data > second_max) {
   
            // else if current node value is
            // greater than second_max, set
            // second_max as node value
            second_max = head.data;
        }
   
        // move the head pointer to next node
        head = head.next;
    }
   
    // Print the largest
    // and second largest value
    System.out.print("Largest = "
         + max +"\n");
    System.out.print("Second Largest = "
         + second_max +"\n");
}
   
// Driver code
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);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the largest and
// second largest element in a Linked List
using System;
  
class GFG{
    
// Link list node
class Node {
    public int data;
    public Node next;
   
};
    
// Function to push the node at the
// beginning of the linked list
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;
}
    
// Function to print the largest
// and second largest element
static void findLargestAndSecondLargest(Node head)
{
    // initialise max and second max using
    // first two nodes of linked list
    int val1 = head.data,
        val2 = head.next.data,
        max = Math.Max(val1, val2),
        second_max = Math.Min(val1, val2);
    
    // move the head pointer to 3rd node
    head = head.next.next;
    
    // iterate over rest of linked list
    while (head != null) {
    
        if (head.data > max) {
    
            // If current node value is greater
            // than max, then set second_max as
            // current max value and max as
            // current node value
            second_max = max;
            max = head.data;
        }
        else if (head.data > second_max) {
    
            // else if current node value is
            // greater than second_max, set
            // second_max as node value
            second_max = head.data;
        }
    
        // move the head pointer to next node
        head = head.next;
    }
    
    // Print the largest
    // and second largest value
    Console.Write("Largest = "
         + max +"\n");
    Console.Write("Second Largest = "
         + second_max +"\n");
}
    
// Driver code
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);
}
}
  
// This code is contributed by Princi Singh

chevron_right


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).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, princi singh

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.