# Sum and Product of the nodes of a Singly Linked List which are divisible by K

Given a singly linked list. The task is to find the sum and product of all of the nodes of the given linked list which are divisible by a given number k.

**Examples**:

Input: List = 7->60->8->40->1 k = 10Output: Product = 2400, Sum = 100 Product of nodes: 60 * 40 = 2400Input: List = 15->7->3->9->11->5 k = 5Output: Product = 75, Sum = 20

**Algorithm:**

- Initialize a pointer
**ptr**with the head of the linked list, a**product**variable with 1 and a**sum**variable with 0. - Start traversing the linked list using a loop until all the nodes get traversed.
- For every node:
- Multiply the value of the current node to the product if current node is divisible by k.
- Add the value of the current node to the sum if current node is divisible by k.

- Increment the pointer to the next node of linked list i.e. ptr = ptr ->next.
- Repeat the above steps until end of linked list is reached.
- Finally, print the product and sum.

Below is the implementation of the above approach:

`// C++ implementation to find the product ` `// and sum of nodes which are divisible by k ` ` ` `#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 and sum of ` `// nodes which are divisible by k ` `// of the given linked list ` `void` `productAndSum(` `struct` `Node* head, ` `int` `k) ` `{ ` ` ` `// Pointer to traverse the list ` ` ` `struct` `Node* ptr = head; ` ` ` ` ` `int` `product = 1; ` `// Variable to store product ` ` ` `int` `sum = 0; ` `// Variable to store sum ` ` ` ` ` `// Traverse the list and ` ` ` `// calculate the product ` ` ` `// and sum ` ` ` `while` `(ptr != NULL) { ` ` ` `if` `(ptr->data % k == 0) { ` ` ` `product *= ptr->data; ` ` ` `sum += ptr->data; ` ` ` `} ` ` ` ` ` `ptr = ptr->next; ` ` ` `} ` ` ` ` ` `// Print the product and sum ` ` ` `cout << ` `"Product = "` `<< product << endl; ` ` ` `cout << ` `"Sum = "` `<< sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// create linked list 70->6->8->4->10 ` ` ` `push(&head, 70); ` ` ` `push(&head, 6); ` ` ` `push(&head, 8); ` ` ` `push(&head, 4); ` ` ` `push(&head, 10); ` ` ` ` ` `int` `k = 10; ` ` ` ` ` `productAndSum(head, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Product = 700 Sum = 80

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

## Recommended Posts:

- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Product of the nodes of a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Product of all nodes in a doubly linked list divisible by a given number K
- Sum of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Reverse alternate K nodes in a Singly Linked List
- Find the common nodes in two singly linked list
- Delete all Prime Nodes from a Circular Singly Linked List
- 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 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.