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.
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.
The second smallest element is 10
Time complexity : O(n)
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Find smallest and largest elements in singly linked list
- Move first element to end of a given Linked List
- Majority element in a linked list
- Find the first duplicate element in the linked list
- Find the Second Largest Element in a Linked List
- Move last element to front of a given Linked List | Set 2
- Find a peak element in Linked List
- Rearrange a linked list in to alternate first and last element
- Move all occurrences of an element to end in a linked list
- Move last element to front of a given Linked List
- Search an element in a Linked List (Iterative and Recursive)
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Merge a linked list into another linked list at alternate positions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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