Minimum and Maximum Prime Numbers of a Singly Linked List Last Updated : 07 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a singly linked list containing N nodes, the task is to find the minimum and maximum prime number. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Minimum : 7 Maximum : 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Minimum : 2 Maximum : 3 Approach: 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: C++ // C++ implementation to find minimum // and maximum prime number of // the singly linked list #include <bits/stdc++.h> using namespace std; // Node of the singly linked list struct Node { int data; Node* next; }; // Function to insert a node at the beginning // of the singly Linked List void push(Node** head_ref, int new_data) { Node* new_node = new Node; new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } // Function to check if a number is prime bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to find maximum and minimum // prime nodes in a linked list void minmaxPrimeNodes(Node** head_ref) { int minimum = INT_MAX; int maximum = INT_MIN; Node* ptr = *head_ref; while (ptr != NULL) { // If current node is prime if (isPrime(ptr->data)) { // Update minimum minimum = min(minimum, ptr->data); // Update maximum maximum = max(maximum, ptr->data); } ptr = ptr->next; } cout << "Minimum : " << minimum << endl; cout << "Maximum : " << maximum << endl; } // Driver program int main() { // start with the empty list Node* head = NULL; // create the linked list // 15 -> 16 -> 7 -> 6 -> 17 push(&head, 17); push(&head, 7); push(&head, 6); push(&head, 16); push(&head, 15); minmaxPrimeNodes(&head); return 0; } Java // Java implementation to find minimum // and maximum prime number of // the singly linked list class GFG { // Node of the singly linked list static class Node { int data; Node next; }; // Function to insert a node at the beginning // of the singly Linked List static Node push(Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head_ref); (head_ref) = new_node; return head_ref; } // Function to check if a number is prime static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to find maximum and minimum // prime nodes in a linked list static void minmaxPrimeNodes(Node head_ref) { int minimum = Integer.MAX_VALUE; int maximum = Integer.MIN_VALUE; Node ptr = head_ref; while (ptr != null) { // If current node is prime if (isPrime(ptr.data)) { // Update minimum minimum = Math.min(minimum, ptr.data); // Update maximum maximum = Math.max(maximum, ptr.data); } ptr = ptr.next; } System.out.println("Minimum : " + minimum ); System.out.println("Maximum : " + maximum ); } // Driver code public static void main(String args[]) { // start with the empty list Node head = null; // create the linked list // 15 . 16 . 7 . 6 . 17 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 16); head = push(head, 15); minmaxPrimeNodes(head); } } // This code is contributed by Arnab Kundu Python3 # Python3 implementation to find minimum # and maximum prime number of # the singly linked list # Structure of a Node class Node: def __init__(self, data): self.data = data self.next = None # Function to insert a node at the beginning # of the singly Linked List def push(head_ref, new_data) : new_node = Node(0) new_node.data = new_data new_node.next = (head_ref) (head_ref) = new_node return head_ref # Function to check if a number is prime def isPrime(n): # Corner cases if (n <= 1) : return False if (n <= 3) : return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0): return False i = i + 6 return True # Function to find maximum and minimum # prime nodes in a linked list def minmaxPrimeNodes(head_ref) : minimum = 999999999 maximum = -999999999 ptr = head_ref while (ptr != None): # If current node is prime if (isPrime(ptr.data)): # Update minimum minimum = min(minimum, ptr.data) # Update maximum maximum = max(maximum, ptr.data) ptr = ptr.next print ("Minimum : ", minimum) print ("Maximum : ", maximum) # Driver code # start with the empty list head = None # create the linked list # 15 . 16 . 7 . 6 . 17 head = push(head, 17) head = push(head, 7) head = push(head, 6) head = push(head, 16) head = push(head, 15) minmaxPrimeNodes(head) # This code is contributed by Arnab Kundu C# // C# implementation to find minimum // and maximum prime number of // the singly linked list using System; class GFG { // Node of the singly linked list public class Node { public int data; public Node next; }; // Function to insert a node at the beginning // of the singly Linked List static Node push(Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head_ref); (head_ref) = new_node; return head_ref; } // Function to check if a number is prime static bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to find maximum and minimum // prime nodes in a linked list static void minmaxPrimeNodes(Node head_ref) { int minimum = int.MaxValue; int maximum = int.MinValue; Node ptr = head_ref; while (ptr != null) { // If current node is prime if (isPrime(ptr.data)) { // Update minimum minimum = Math.Min(minimum, ptr.data); // Update maximum maximum = Math.Max(maximum, ptr.data); } ptr = ptr.next; } Console.WriteLine("Minimum : " + minimum); Console.WriteLine("Maximum : " + maximum); } // Driver code public static void Main() { // start with the empty list Node head = null; // create the linked list // 15 . 16 . 7 . 6 . 17 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 16); head = push(head, 15); minmaxPrimeNodes(head); } } // This code is contributed by Princi Singh JavaScript <script> // javascript implementation to find minimum // and maximum prime number of // the singly linked list // Node of the singly linked list class Node { constructor(val) { this.data = val; this.next = null; } } // Function to insert a node at the beginning // of the singly Linked List function push(head_ref , new_data) { var new_node = new Node(); new_node.data = new_data; new_node.next = (head_ref); (head_ref) = new_node; return head_ref; } // Function to check if a number is prime function isPrime(n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to find maximum and minimum // prime nodes in a linked list function minmaxPrimeNodes(head_ref) { var minimum = Number.MAX_VALUE; var maximum = Number.MIN_VALUE; var ptr = head_ref; while (ptr != null) { // If current node is prime if (isPrime(ptr.data)) { // Update minimum minimum = Math.min(minimum, ptr.data); // Update maximum maximum = Math.max(maximum, ptr.data); } ptr = ptr.next; } document.write("Minimum : " + minimum); document.write("<br/>Maximum : " + maximum); } // Driver code // start with the empty list var head = null; // create the linked list // 15 . 16 . 7 . 6 . 17 head = push(head, 17); head = push(head, 7); head = push(head, 6); head = push(head, 16); head = push(head, 15); minmaxPrimeNodes(head); // This code contributed by umadevi9616 </script> OutputMinimum : 7 Maximum : 17 Time Complexity: O(N), where N is the number of nodes in the linked list. Comment More infoAdvertise with us Next Article Minimum and Maximum Prime Numbers of a Singly Linked List V VishalBachchas Follow Improve Article Tags : Linked List Technical Scripter DSA Technical Scripter 2018 Prime Number +1 More Practice Tags : Linked ListPrime Number Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like