Find the Second Smallest Element in a Linked List

Find the second smallest element of linked list with minimum iteration. We are assume that linked list is combination of integer value nodes. And linked list node can be hold any integer value like negative or positive.

implementation approach : We can solve this problem in a two ways.

Method A : First approach are sorted a linked list element in ascending order then find second smallest element. But note that is process are required modification of existing linked list. And arrangement of linked list element in sorted view it will take more than O(n) time to sort element. So we can solve this problem in second approach.

Method B: Suppose we are find first smallest element of linked list, then we can solve this problem very easily by using linked list iteration front node to last node and compare the node values.

We can modified this approach to finding a second last node of linked list. We are tack two integers or two pointers as you wise you can take any one. Our goal is to find second smallest node by using first smallest element. Before view the solution of this problem. Apply your logic and write an algorithm which will satisfy the following test cases.

1) When linked list are empty then display proper message like ( empty linked list etc).

2) When linked list are contain only one nodes then we cannot find second smallest element. So in this case also display a message like (Only one node of linked list etc).

3) When every similar nodes are exist in linked list in this situation, smallest node is an second smallest value.

Linked list : 1->1->1->NULL
Result : 1

We can modified this situation when all similar nodes. But in this case above result are accepting.

4) There can be possible duplicates nodes are exist in linked list. In this situation there are also possible to two smallest element. for example.

Linked list : 1->2->1->3->NULL (2 smallest 1)
Result : 2

We are find other second smallest node which are existing in linked list.

Suppose we are inserted the following (7, 1, 4, 2, 5, 3 , 1 ) node in a sequence.

Find the Second Smallest Element in a Linked List

Here given code implementation process.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved