Find smallest and largest elements in singly linked list
Given a singly linked list of n nodes and find the smallest and largest elements in linked list. Show Recommended: Please try your approach on {IDE} first, before moving on to the solution. The idea is to traverse the linked list while head not equal to NULL and initialise the max and min variable to INT_MIN and INT_MAX respectively. After that check a condition that if max value is less then head value is assigned to max or min value is greater then head value is assigned to min otherwise head point to next node. Continue this process until head not equal to NULL. C++
Java
Python
C#
Javascript
Output: Linked list is : 17 -> 22 -> 13 -> 14 -> 15 -> NULL Maximum element in linked list: 22 Minimum element in linked list: 13This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Article Tags : Linked List Practice Tags : Linked List Recommended: Please try your approach on {IDE} first, before moving on to the solution.The idea is to traverse the linked list while head not equal to NULL and initialise the max and min variable to INT_MIN and INT_MAX respectively. After that check a condition that if max value is less then head value then head value is assign to max or min value is greater then head value then head value is assign to min otherwise head point to next node. Continue this process until head not equal to NULL. Program to find the maximum and minimum value node from a singly linked listExplanationIn this program, we need to find out the minimum and maximum value node in the given singly linked list. We will maintain two variables min and max. Min will hold minimum value node, and max will hold maximum value node. In the above example, 1 will be minimum value node and 8 will be maximum value node. The algorithm to find the maximum and minimum node is given below. Algorithm
SolutionPythonOutput: Minimum value node in the list: 1 Maximum value node in the list: 8 COutput: Minimum value node in the list: 1 Maximum value node in the list: 8 JAVAOutput: Minimum value node in the list: 1 Maximum value node in the list: 8 C#Output: Minimum value node in the list: 1 Maximum value node in the list: 8 PHPOutput: Minimum value node in the list: 1 Maximum value node in the list: 8 Find the Second Smallest Element in a Linked ListFind 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 : 1We 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 : 2We 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. Here given code implementation process.
|