Given a singly linked list. The task is to find the product of all of the nodes of the given linked list.
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
- 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# implementation to find the product of
// nodes of the Linked List
// A Linked list node
public class Node
public int data;
public 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;
// 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
// Driver Code
public static void Main(String args)
Node head = null;
// create linked list 18.104.22.168.1
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
Console.WriteLine(“Product = ” +
// This code is contributed by Rajput-Ji
Product = 1344
Time Complexity: O(N), where N is the number of nodes in the linked list.
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Sum of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Find the common nodes in two singly linked list
- Delete all Non-Prime Nodes from a Singly Linked List
- Reverse alternate K nodes in a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Update adjacent nodes if the current node is zero in a Singly Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Linked List Product of Nodes Between 0s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.