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.

Updated on: 10-Aug-2022

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements