- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to find Second Smallest Element in a Linked List

An ordered collection of data elements, each with a link to its next element (and sometimes its predecessor), Suppose there is a linked list, then we need to find the second smallest element. The below are the following scenarios.

**Let’s assume some simple input and output scenarios**

Let’s assume this scenario that we are having a linked list contains elements in it are **"8->4->6->2->9,".** Then after iterating entire linked list, the second smallest element will be 8.

Input = 8->4->6->2->9 Output = 8

Programing way of implementing the linked list

Node* head = new Node(8); head->next = new Node(4); head->next->next = new Node(6); head->next->next->next = new Node(2); head->next->next->next->next = new Node(9); result = solve(head);

Consider another scenario, Where every element in the linked list is having same value. After iterating all the elements, there will be no second smallest element found in the linked list. As the elements in the linked list consisting same values in every element.

Input = 1->1->1->1->1->1 Output = There is no second smallest element in Linked list

## Algorithm

The following are steps to be followed to perform the task

Assign two variables (S1,S2)

S1 will be holding the smallest element in the linked list

S2 will be holding the second smallest element in the linked list.

Every iteration goes on the smallest element will move to S1 and it moves to S2 whenever it encounters

New smaller will become the smallest(S1), when smallest(S1) is less than new smaller value.

The new smaller will become smaller(S1) and smaller(S1) will move into second smaller(S2).

After every possible traversal, the second smallest element in the final output will be resulted as output.

## Example

In the C++ implementation, we can keep 2 variables. 1 for the smallest and other for the second smallest and then traverse the linked list and update those variables with the smallest move to the second smallest every time we encounter a smaller element, and the new smaller becomes the smallest. So every time an element is smaller than the smallest, the second smallest becomes smallest, and the smallest becomes the new element. If not, we compare the second smallest and determine if the current element is smaller than the second smallest, and we update accordingly.

#include <iostream> using namespace std; class Node { public: int val; Node *next; Node(int val) { this->val = val; next = NULL; } }; int solve(Node* root) { int s1=root->val, s2=root->val; while(root) { if(root->val <= s1) { s2 = s1; s1 = root->val; } else if(root->val < s2) { s2 = root->val; } root = root->next; } return s2; } int main() { Node* head = new Node(5); head->next = new Node(8); head->next->next = new Node(9); head->next->next->next = new Node(2); head->next->next->next->next = new Node(4); cout << "Second smallest element in the linked list is : " << solve(head); return 0; }

## Output

Second smallest element in the linked list is: 4

## Conclusion

The time complexity is O(n) as we traverse the linked list once. If you found the above information useful, then please make sure you visit our official website to know more about programming related topics.

- Related Questions & Answers
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Find the Second Largest Element in a Linked List in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Python Program to Find the Largest Element in a Doubly Linked List
- Find a peak element in Linked List in C++
- How to find middle element in a linked list in android?
- Program to find linked list intersection from two linked list in Python
- Python program to find the smallest number in a list
- Find smallest and largest elements in singly linked list in C++
- Python program to find the second largest number in a list
- Python program to search an element in a Circular Linked List
- Python program to search an element in a doubly linked list
- Python program to sort a list according to the second element in sublist
- Find the second last node of a linked list in single traversal in C++
- JavaScript: How to Find the Middle Element of a Linked List?