Given a Linked list of integer data. The task is to write a program that efficiently finds the second largest element present in the Linked List.
Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second largest element is 34. Input : List = 10 -> 5 -> 10 Output : The second largest element is 5.
A Simple Solution will be to first sort the linked list in descending 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 maximum element. In the second traversal find the greatest element less 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 largest element in a single traversal.
Below is the complete algorithm for doing this:
1) Initialize two variables first and second to INT_MIN as, first = second = INT_MIN 2) Start traversing the Linked List, a) If the current element in Linked List say list[i] is greater than first. Then update first and second as, second = first first = list[i] b) If the current element is in between first and second, then update second to store the value of current variable as second = list[i] 3) Return the value stored in second node.
Below is the implementation of the above approach:
The second largest element is 34
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.
- Find the largest and second largest value in a Linked List
- Second Smallest Element in a Linked List
- Create new linked list from two given linked list with greater element at each node
- Find the second last node of a linked list in single traversal
- Find extra node in the second Linked list
- Merge first half and reversed second half of the linked list alternatively
- Create a linked list from two linked lists by choosing max element at each position
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Find smallest and largest elements in singly linked list
- Find the largest node in Doubly linked list
- Find a peak element in Linked List
- Find the first duplicate element in the linked list
- Find the element in a linked list with frequency at least N/3
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
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.