Given a singly linked list of n nodes and find the smallest and largest elements in linked list.
Input : 15 14 13 22 17 Output : Linked list are: 17 -> 22 -> 13 -> 14 -> 15 -> NULL Maximum element in linked list: 22 Minimum element in linked list: 13 Input : 20 25 23 68 54 13 45 Output : Linked list are: 45 -> 13 -> 54 -> 68 -> 23 -> 25 -> 20 -> NULL Maximum element in linked list: 68 Minimum element in linked list: 13
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.
Linked list is : 17 -> 22 -> 13 -> 14 -> 15 -> NULL Maximum element in linked list: 22 Minimum element in linked list: 13
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find the middle of a given linked list in C and Java
- Program for n'th node from the end of a Linked List
- Write a function to get Nth node in a Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Detect loop in a linked list
- Write a function to delete a Linked List
- Write a function that counts the number of times a given int occurs in a Linked List
- Reverse a linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Write a function to get the intersection point of two Linked Lists
- Function to check if a singly linked list is palindrome
- The Great Tree-List Recursion Problem.
- Clone a linked list with next and random pointer | Set 1
- Memory efficient doubly linked list
- Given a linked list which is sorted, how will you insert in sorted way