# Find the Second Largest Element in a Linked List

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.

**Examples**:

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:

`// C++ program to print second largest ` `// value in a linked list ` `#include <climits> ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` `// A linked list node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// Function to add a node at the ` `// begining of Linked List ` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `/* allocate node */` ` ` `struct` `Node* new_node = (` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `/* put in the data */` ` ` `new_node->data = new_data; ` ` ` ` ` `/* link the old list off the new node */` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `/* move the head to point to the new node */` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// Function to count size of list ` `int` `listSize(` `struct` `Node* node) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `while` `(node != NULL) { ` ` ` `count++; ` ` ` ` ` `node = node->next; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Function to print the second ` `// largest element ` `void` `print2largest(` `struct` `Node* head) ` `{ ` ` ` `int` `i, first, second; ` ` ` ` ` `int` `list_size = listSize(head); ` ` ` ` ` `/* There should be atleast two elements */` ` ` `if` `(list_size < 2) { ` ` ` `cout << ` `"Invalid Input"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `first = second = INT_MIN; ` ` ` ` ` `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_MIN) ` ` ` `cout << ` `"There is no second largest element\n"` `; ` ` ` `else` ` ` `cout << ` `"The second largest 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); ` ` ` ` ` `print2largest(start); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The second largest element is 34

## Recommended Posts:

- Find the largest node in Doubly linked list
- Find smallest and largest elements in singly linked list
- Find a peak element in Linked List
- Second Smallest Element in a Linked List
- Move first element to end of a given Linked List
- Majority element in a linked list
- Move last element to front of a given 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
- 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
- Find the sum of last n nodes of the given Linked List
- Find the fractional (or n/k - th) node in linked list
- Find unique elements in linked list
- Find the product of first k nodes of the given Linked List

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.