Product of all prime nodes in a Doubly Linked List
Last Updated :
14 Mar, 2023
Given a doubly linked list containing N nodes. The task is to find the product of all prime nodes.
Example:
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
Approach:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
int prodOfPrime(Node** head_ref)
{
Node* ptr = *head_ref;
Node* next;
int prod = 1;
while (ptr != NULL) {
next = ptr->next;
if (isPrime(ptr->data))
prod = prod * ptr->data;
ptr = next;
}
return prod;
}
int main()
{
Node* head = NULL;
push(&head, 17);
push(&head, 6);
push(&head, 7);
push(&head, 16);
push(&head, 15);
int prod = prodOfPrime(&head);
cout << "Product of Prime Nodes : " << prod;
return 0;
}
|
Java
class Node
{
int data;
Node next, prev;
Node( int d)
{
data = d;
next = null ;
prev = null ;
}
}
class DLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
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 ;
}
static int prodOfPrime(Node node)
{
int prod = 1 ;
while (node != null )
{
if (isPrime(node.data))
prod *= node.data;
node = node.next;
}
return prod;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 17 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 9 );
head = push(head, 10 );
head = push(head, 16 );
head = push(head, 15 );
int prod = prodOfPrime(head);
System.out.println( "Product of Prime Nodes: " + prod);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node.prev = None
new_node. next = (head_ref)
if ((head_ref) ! = None ):
(head_ref).prev = new_node
(head_ref) = new_node
return head_ref
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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 + = 6 ;
return True
def prodOfPrime(head_ref):
ptr = head_ref
next = None
prod = 1
while (ptr ! = None ):
next = ptr. next
if (isPrime(ptr.data)):
prod = prod * ptr.data
ptr = next
return prod
if __name__ = = "__main__" :
head = None
head = push(head, 17 )
head = push(head, 6 )
head = push(head, 7 )
head = push(head, 16 )
head = push(head, 15 )
prod = prodOfPrime(head)
print ( "Product of Prime Nodes : " , prod)
|
C#
using System;
public class Node
{
public int data;
public Node next, prev;
public Node( int d)
{
data = d;
next = null ;
prev = null ;
}
}
class DLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
static Boolean isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
static int prodOfPrime(Node node)
{
int prod = 1;
while (node != null )
{
if (isPrime(node.data))
prod *= node.data;
node = node.next;
}
return prod;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
int prod = prodOfPrime(head);
Console.WriteLine( "Product of Prime Nodes: " + prod);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
function push(head , data) {
var newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
function isPrime(n) {
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 prodOfPrime(node) {
var prod = 1;
while (node != null ) {
if (isPrime(node.data))
prod *= node.data;
node = node.next;
}
return prod;
}
var head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
var prod = prodOfPrime(head);
document.write( "Product of Prime Nodes: " + prod);
</script>
|
Output
Product of Prime Nodes : 119
Complexity Analysis:
- Time Complexity: O(N), where N is the number of nodes.
- Space Complexity: O(1) because using constant variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...