Second Smallest Element in a Linked List
Given a Linked list of integer data. The task is to write a program that efficiently finds the second smallest element present in the Linked List.
Examples:
Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second smallest element is 10. Input : List = 10 -> 5 -> 10 Output : The second largest element is 10.
A Simple Solution will be to first sort the linked list in ascending order and then print the second element from the sorted linked list. The time complexity of this solution is O(nlogn).
A Better Solution is to traverse the Linked list twice. In the first traversal find the minimum element. In the second traversal find the smallest element greater than the element obtained in first traversal. The time complexity of this solution is O(n).
A more Efficient Solution can be to find the second smallest element in a single traversal.
C++
// C++ program to print second smallest// value in a linked list#include <climits>#include <iostream>using namespace std;// A linked list nodestruct Node { int data; struct Node* next;};// Function to add a node at the// beginning of Linked Listvoid push(struct Node** head_ref, int new_data){ struct Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node;}// Function to print the second// smallest elementvoid print2smallest(struct Node* head){ int first = INT_MAX, second = INT_MAX; struct Node* temp = head; while (temp != NULL) { if (temp->data < first) { second = first; first = temp->data; } // If current node's data is in between // first and second then update second else if (temp->data < second && temp->data != first) second = temp->data; temp = temp->next; } if (second == INT_MAX) cout << "There is no second smallest element\n"; else cout << "The second smallest element is " << second;}// Driver program to test above functionint main(){ struct Node* start = NULL; /* The constructed linked list is: 12 -> 35 -> 1 -> 10 -> 34 -> 1 */ push(&start;, 1); push(&start;, 34); push(&start;, 10); push(&start;, 1); push(&start;, 35); push(&start;, 12); print2smallest(start); return 0;} |
Java
// Java program to print second smallest// value in a linked listclass GFG{// A linked list nodestatic class Node{ int data; Node next;};// Function to add a node at the// beginning of Linked Liststatic 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 new_node;}// Function to print the second// smallest elementstatic void print2smallest(Node head){ int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE; Node temp = head; while (temp != null) { if (temp.data < first) { second = first; first = temp.data; } // If current node's data is in between // first and second then update second else if (temp.data < second && temp.data != first) second = temp.data; temp = temp.next; } if (second == Integer.MAX_VALUE) System.out.print("There is no second smallest element\n"); else System.out.print("The second smallest element is " + second);}// Driver codepublic static void main(String[] args){ Node start = null; /* The constructed linked list is: 12.35.1.10.34.1 */ start = push(start, 1); start = push(start, 34); start = push(start, 10); start = push(start, 1); start = push(start, 35); start = push(start, 12); print2smallest(start);}}// This code is contributed by PrinciRaj1992 |
Python3
# Python3 program to print second smallest# value in a linked list# A linked list nodeclass Node : def __init__(self): self.data = 0 self.next = None# Function to add a node at the# beginning of Linked Listdef push(head_ref, new_data): new_node = Node() new_node.data = new_data new_node.next = head_ref head_ref = new_node return new_node# Function to print the second# smallest elementdef print2smallest(head): first = 32676 second = 32676 temp = head while (temp != None): if (temp.data < first): second = first first = temp.data # If current node's data is in between # first and second then update second elif (temp.data < second and temp.data != first): second = temp.data temp = temp.next if (second == 32676): print("There is no second smallest element") else: print("The second smallest element is " , second)# Driver codestart = None# The constructed linked list is:# 12.35.1.10.34.1start = push(start, 1)start = push(start, 34)start = push(start, 10)start = push(start, 1)start = push(start, 35)start = push(start, 12)print2smallest(start)# This code is contributed by Arnab Kundu |
C#
// C# program to print second smallest// value in a linked listusing System;class GFG{// A linked list nodeclass Node{ public int data; public Node next;};// Function to add a node at the// beginning of Linked Liststatic 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 new_node;}// Function to print the second// smallest elementstatic void print2smallest(Node head){ int first = int.MaxValue, second = int.MaxValue; Node temp = head; while (temp != null) { if (temp.data < first) { second = first; first = temp.data; } // If current node's data is in between // first and second then update second else if (temp.data < second && temp.data != first) second = temp.data; temp = temp.next; } if (second == int.MaxValue) Console.Write("There is no second smallest element\n"); else Console.Write("The second smallest element is " + second);}// Driver codepublic static void Main(String[] args){ Node start = null; /* The constructed linked list is: 12 -> 35 -> 1 -> 10 -> 34 -> 1 */ start = push(start, 1); start = push(start, 34); start = push(start, 10); start = push(start, 1); start = push(start, 35); start = push(start, 12); print2smallest(start);}}// This code is contributed by Rajput-Ji |
Javascript
<script>// Javascript program to print second smallest// value in a linked list// Structure to represent a node of the treeclass Node { constructor() { this.data = 0; this.next = null; } } // Function to add a node at the// beginning of Linked Listfunction 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 new_node;}// Function to print the second// smallest elementfunction print2smallest( head){ let first = Number.MAX_VALUE , second = Number.MAX_VALUE ; var temp = head; while (temp != null) { if (temp.data < first) { second = first; first = temp.data; } // If current node's data is in between // first and second then update second else if (temp.data < second && temp.data != first) second = temp.data; temp = temp.next; } if (second == Number.MAX_VALUE ) document.write("There is no second smallest element" + "</br>"); else document.write("The second smallest element is " + second);}// Driver Codevar start = null;/* The constructed linked list is:12.35.1.10.34.1 */start = push(start, 1);start = push(start, 34);start = push(start, 10);start = push(start, 1);start = push(start, 35);start = push(start, 12);print2smallest(start); // This code is contributed by jana_sayantan.</script> |
The second smallest element is 10
Time complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


