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.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++ program to print second smallest // value in a linked list #include #include    using namespace std;    // A linked list node struct Node {     int data;     struct Node* next; };    // Function to add a node at the // beginning of Linked List void 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 element void 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 function int 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; }

Output:

The second smallest element is 10

Time complexity : O(n)

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 : VishalBachchas, nidhi_biet