Product of all nodes in a doubly linked list divisible by a given number K

Given a doubly-linked list containing N nodes and given a number K. The task is to find the product of all such nodes which are divisible by K.

Examples:

Input : List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17
        K = 3
Output : Product = 810

Input : List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9
        K = 2
Output : Product = 384

The idea is to traverse the doubly linked list and check the nodes one by one. If a node’s value is divisible by K then multiply that node’s value with the product so far and continue this process while the end of the list is not reached.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find product of nodes in a
// doubly linked list divisible by K
  
#include <iostream>
using namespace std;
  
// Doubly linked list node
struct Node {
    int data;
    Node *prev, *next;
};
  
// function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
    // allocate node
    Node* new_node = (Node*)malloc(sizeof(struct Node));
  
    // put in the data
    new_node->data = new_data;
  
    // since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
  
    // link the old list off the new node
    new_node->next = (*head_ref);
  
    // change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
  
    // move the head to point to the new node
    (*head_ref) = new_node;
}
  
// Function to find the product of all the nodes from
// the doubly linked list that is divisible by K
int productOfNode(Node** head_ref, int K)
{
    Node* ptr = *head_ref;
    Node* next;
  
    int product = 1;
  
    // Travese list till last node
    while (ptr != NULL) {
        next = ptr->next;
        // check is node value divided by K
        // if true then add in sum
        if (ptr->data % K == 0)
            product *= ptr->data;
        ptr = next;
    }
  
    // Return product of nodes which
    // are divisible by K
    return product;
}
  
// Driver Code
int main()
{
    // start with the empty list
    Node* head = NULL;
  
    // create the doubly linked list
    // 15 16 10 9 6 7 17
    push(&head, 17);
    push(&head, 7);
    push(&head, 6);
    push(&head, 9);
    push(&head, 10);
    push(&head, 16);
    push(&head, 15);
  
    int K = 3;
  
    int prod = productOfNode(&head, K);
  
    cout << "Product = " << prod;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find product of nodes in a 
// doubly linked list divisible by K 
class GFG
{
  
// Doubly linked list node 
static class Node
    int data; 
    Node prev, next; 
}; 
  
// function to insert a node at the beginning 
// of the Doubly Linked List 
static Node push(Node head_ref, int new_data) 
    // allocate node 
    Node new_node = new Node(); 
  
    // put in the data 
    new_node.data = new_data; 
  
    // since we are adding at the beginning, 
    // prev is always null 
    new_node.prev = null
  
    // link the old list off the new node 
    new_node.next = (head_ref); 
  
    // change prev of head node to new node 
    if ((head_ref) != null
        (head_ref).prev = new_node; 
  
    // move the head to point to the new node 
    (head_ref) = new_node; 
    return head_ref;
  
// Function to find product of all the nodes from 
// the doubly linked list that are divisible by K 
static int productOfNode(Node head_ref, int K) 
    Node ptr = head_ref; 
    Node next; 
  
    int product = 1
  
    // Travese list till last node 
    while (ptr != null)
    
        next = ptr.next; 
          
        // check is node value divided by K 
        // if true then add in sum 
        if (ptr.data % K == 0
            product *= ptr.data; 
        ptr = next; 
    
  
    // Return product of nodes which 
    // are divisible by K 
    return product; 
  
// Driver Code 
public static void main(String args[])
    // start with the empty list 
    Node head = null
  
    // create the doubly linked list 
    // 15 16 10 9 6 7 17 
    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 K = 3
  
    int prod = productOfNode(head, K); 
  
    System.out.println( "Product = " + prod); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find product of nodes in a 
// doubly linked list divisible by K 
using System;
  
class GFG
{
  
// Doubly linked list node 
public class Node
    public int data; 
    public Node prev, next; 
}; 
  
// function to insert a node at the beginning 
// of the Doubly Linked List 
static Node push(Node head_ref, int new_data) 
    // allocate node 
    Node new_node = new Node(); 
  
    // put in the data 
    new_node.data = new_data; 
  
    // since we are adding at the beginning, 
    // prev is always null 
    new_node.prev = null
  
    // link the old list off the new node 
    new_node.next = (head_ref); 
  
    // change prev of head node to new node 
    if ((head_ref) != null
        (head_ref).prev = new_node; 
  
    // move the head to point to the new node 
    (head_ref) = new_node; 
    return head_ref;
  
// Function to find product of all the nodes from 
// the doubly linked list that are divisible by K 
static int productOfNode(Node head_ref, int K) 
    Node ptr = head_ref; 
    Node next; 
  
    int product = 1; 
  
    // Travese list till last node 
    while (ptr != null)
    
        next = ptr.next; 
          
        // check is node value divided by K 
        // if true then add in sum 
        if (ptr.data % K == 0) 
            product *= ptr.data; 
        ptr = next; 
    
  
    // Return product of nodes which 
    // are divisible by K 
    return product; 
  
// Driver Code 
public static void Main(String []args)
    // start with the empty list 
    Node head = null
  
    // create the doubly linked list 
    // 15 16 10 9 6 7 17 
    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 K = 3; 
  
    int prod = productOfNode(head, K); 
  
    Console.WriteLine( "Product = " + prod); 
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

Product = 810

Time Complexity: O(N), where N is the number of nodes.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.