Given a singly linked list containing N nodes, the task is to find the minimum and maximum prime number.
Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Minimum : 7 Maximum : 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Minimum : 2 Maximum : 3
- The idea is to traverse the linked list to the end and initialize the max and min variable to INT_MIN and INT_MAX respectively.
- Check if the current node is prime or not. If Yes:
- If current node’s value is greater than max then assign current node’s value to max.
- If current node’s value is less than min then assign current node’s value to min.
- Repeat above step until end of list is reached.
Below is the implementation of above idea:
Minimum : 7 Maximum : 17
Time Complexity: O(N), where N is the number of nodes in the linked list.
- Find minimum and maximum elements in singly Circular Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Minimum and Maximum prime numbers in an array
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Convert Singly Linked List to XOR Linked List
- Sum of the nodes of a Singly Linked List
- QuickSort on Singly Linked List
- Alternating split of a given Singly Linked List | Set 1
- Circular Singly Linked List | Insertion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.