Product of the nodes of a Singly Linked List
Last Updated :
11 Oct, 2023
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:
- Initialize a pointer ptr 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.
- Multiply the value of the current node to the product i.e. product *= ptr -> data.
- Increment the pointer to the next node of linked list i.e. ptr = ptr ->next.
- Repeat the above two steps until end of linked list is reached.
- Finally, return the product.
Below is the implementation of above algorithm:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int productOfNodes( struct Node* head)
{
struct Node* ptr = head;
int product = 1;
while (ptr != NULL) {
product *= ptr->data;
ptr = ptr->next;
}
return product;
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Product = " << productOfNodes(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node *head = NULL;
void push( struct Node *node, int new_data) {
struct Node * new = malloc ( sizeof ( struct Node));
new -> data = new_data;
new -> next = head;
head = new ;
}
int productOfNodes() {
struct Node *tr = head;
int product = 1;
while (tr != NULL) {
product *= tr -> data;
tr = tr -> next;
}
return product;
}
int main() {
push(head, 1);
push(head, 4);
push(head, 8);
push(head, 6);
push(head, 7);
int ans = productOfNodes();
printf ( "Product = %d" , ans);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
class LinkedList {
Node head;
Node push( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
return head;
}
int productOfNodes()
{
Node ptr = head;
int product = 1 ;
while (ptr != null ) {
product *= ptr.data;
ptr = ptr.next;
}
return product;
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push( 1 );
llist.push( 4 );
llist.push( 8 );
llist.push( 6 );
llist.push( 7 );
System.out.println( "Product = "
+ llist.productOfNodes());
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head, data):
if not head:
return Node(data)
new_node = Node(data)
new_node. next = head
head = new_node
return head
def productOfNodes(head):
ptr = head
product = 1
while (ptr):
product * = ptr.data
ptr = ptr. next
return product
if __name__ = = '__main__' :
head = None
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 4 )
head = push(head, 1 )
print ( "Product = {}" . format (productOfNodes(head)))
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
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;
}
static int productOfNodes(Node head)
{
Node ptr = head;
int product = 1;
while (ptr != null ) {
product *= ptr.data;
ptr = ptr.next;
}
return product;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
Console.WriteLine( "Product = "
+ productOfNodes(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
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 productOfNodes(head) {
var ptr = head;
var product = 1;
while (ptr != null ) {
product *= ptr.data;
ptr = ptr.next;
}
return product;
}
var head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
document.write( "Product = " + productOfNodes(head));
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the number of nodes in the linked list.
- Auxiliary Space: O(1)
Recursive Approach:
- If the head pointer is NULL, return 1.
- Otherwise, recursively call the productOfNodes function on the next node of the linked list.
- Multiply the data value of the current node with the return value from the recursive call.
- Return the final product.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int productOfNodes( struct Node* head)
{
if (head == NULL) {
return 1;
}
else {
return (head->data * productOfNodes(head->next));
}
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Product = " << productOfNodes(head);
return 0;
}
|
Java
class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class Main {
static Node push(Node headRef, int newData)
{
Node newNode = new Node(newData);
newNode.next = headRef;
headRef = newNode;
return headRef;
}
static int productOfNodes(Node head)
{
if (head == null ) {
return 1 ;
}
else {
return (head.data * productOfNodes(head.next));
}
}
public static void main(String[] args)
{
Node head = null ;
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));
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
return new_node
def product_of_nodes(head):
if head is None :
return 1
else :
return head.data * product_of_nodes(head. next )
if __name__ = = "__main__" :
head = None
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 4 )
head = push(head, 1 )
print ( "Product =" , product_of_nodes(head))
|
C#
using System;
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class LinkedListOperations
{
static Node Push(Node headRef, int newData)
{
Node newNode = new Node(newData);
newNode.next = headRef;
headRef = newNode;
return headRef;
}
static int ProductOfNodes(Node head)
{
if (head == null )
{
return 1;
}
else
{
return (head.data * ProductOfNodes(head.next));
}
}
public static void Main( string [] args)
{
Node head = null ;
head = Push(head, 7);
head = Push(head, 6);
head = Push(head, 8);
head = Push(head, 4);
head = Push(head, 1);
Console.WriteLine( "Product = " + ProductOfNodes(head));
}
}
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
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 productOfNodes(head) {
if (head == null ){
return 1;
} else {
return (head.data * productOfNodes(head.next));
}
}
var head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
console.log( "Product = " + productOfNodes(head));
|
Output:
Product = 1344
Time Complexity: O(n), where n is the number of nodes in the linked list. This is because the function must visit each node in the linked list exactly once.
Auxiliary Space: O(n), where n is the number of nodes in the linked list. This is because the recursive calls build up a chain of stack frames, one for each node in the linked list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...