Product of the nodes of a Singly Linked List

Given a singly linked list. The task is to find the product of all of the nodes of the given linked list.

Examples:

Input : List = 7->6->8->4->1
Output : Product = 1344
Product of nodes: 7 * 6 * 8 * 4 * 1 = 1344

Input : List = 1->7->3->9->11->5
Output : Product = 10395


Algorithm:

  1. Initialize a pointer ptr with the head of the linked list and a product variable with 1.
  2. Start traversing the linked list using a loop until all the nodes get traversed.
  3. Multiply the value of the current node to the product i.e. product *= ptr -> data.
  4. Increment the pointer to the next node of linked list i.e. ptr = ptr ->next.
  5. Repeat the above two steps until end of linked list is reached.
  6. Finally, return the product.

Below is the implementation of above algorithm:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the product of
// nodes of the Linked List
  
#include <iostream>
using namespace std;
  
// A Linked list node
struct Node {
    int data;
    struct Node* next;
};
  
// Function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = new Node;
  
    /* put in the data */
    new_node->data = new_data;
  
    /* link the old list to the new node */
    new_node->next = (*head_ref);
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
// Function to find the product of
// nodes of the given linked list
int productOfNodes(struct Node* head)
{
    // Pointer to traverse the list
    struct Node* ptr = head;
  
    int product = 1; // Variable to store product
  
    // Traverse the list and
    // calculate the product
    while (ptr != NULL) {
  
        product *= ptr->data;
        ptr = ptr->next;
    }
  
    // Return the product
    return product;
}
  
// Driver Code
int main()
{
    struct Node* head = NULL;
  
    // create linked list 7->6->8->4->1
    push(&head, 7);
    push(&head, 6);
    push(&head, 8);
    push(&head, 4);
    push(&head, 1);
  
    cout << "Product = " << productOfNodes(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the product of 
// nodes of the Linked List 
class GFG 
{
  
// A Linked list node 
static class Node
    int data; 
    Node next; 
}; 
  
// Function to insert a node at the 
// beginning of the 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; 
  
    // link the old list to the new node /
    new_node.next = (head_ref); 
  
    // move the head to point to the new node /
    (head_ref) = new_node;
    return head_ref;
  
// Function to find the product of 
// nodes of the given linked list 
static int productOfNodes( Node head) 
    // Pointer to traverse the list 
    Node ptr = head; 
  
    int product = 1; // Variable to store product 
  
    // Traverse the list and 
    // calculate the product 
    while (ptr != null)
    
  
        product *= ptr.data; 
        ptr = ptr.next; 
    
  
    // Return the product 
    return product; 
  
// Driver Code 
public static void main(String args[])
    Node head = null
  
    // create linked list 7.6.8.4.1 
    head = push(head, 7); 
    head = push(head, 6); 
    head = push(head, 8); 
    head = push(head, 4); 
    head = push(head, 1); 
  
    System.out.println("Product = " + productOfNodes(head)); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


Output:

Product = 1344

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



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.



Improved By : andrew1234