Given a doubly linked list containing N nodes. The task is to find the product of all prime nodes.
Input: List = 15 <=> 16 <=> 6 <=> 7 <=> 17
Output: Product of Prime Nodes: 119
Input: List = 5 <=> 3 <=> 4 <=> 2 <=> 9
Output: Product of Prime Nodes: 30
- Initialize a pointer temp with the head of the linked list and a product variable with 1.
- Start traversing the linked list using a loop until all the nodes get traversed.
- If node value is prime then multiply the value of the current node to the product i.e. product *= current_node-> data.
- Increment the pointer to the next node of linked list i.e. temp = temp -> next.
- Return the product.
Below is the implementation of the above approach:
Product of Prime Nodes : 119
Time Complexity: O(N), where N is the number of nodes.
- Delete all Prime Nodes from a Doubly Linked List
- Product of all nodes in a doubly linked list divisible by a given number K
- Sum and Product of all Prime Nodes of a Singly Linked List
- Rotate Doubly linked list by N nodes
- Delete all the even nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all nodes from the doubly linked list which are divisible by K
- Sum of all nodes in a doubly linked list divisible by a given number K
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Find pairs with given product in a sorted Doubly Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Sum and Product of all the nodes which are less than K in the linked list
- Linked List Product of Nodes Between 0s
- Find the product of first k nodes of the given Linked List
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.