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

**Examples:**

Input:List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 K = 3Output:Sum = 30Input:List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9 K = 2Output:Sum = 20

**Approach:** 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 add that node value to otherwise continue this process while the end of the list is not reached.

Below is the implementation of the above approach:

`// C++ implementation to add ` `// all nodes value which is ` `// divided by any given number K ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Node of the doubly linked list ` `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 begining, ` ` ` `// 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 sum all the nodes ` `// from the doubly linked ` `// list that are divided by K ` `int` `sumOfNode(Node** head_ref, ` `int` `K) ` `{ ` ` ` `Node* ptr = *head_ref; ` ` ` `Node* next; ` ` ` `// variable sum=0 for add nodes value ` ` ` `int` `sum = 0; ` ` ` `// traves 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) ` ` ` `sum += ptr->data; ` ` ` `ptr = next; ` ` ` `} ` ` ` `// return sum of nodes which is divided by K ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `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` `sum = sumOfNode(&head, 3); ` ` ` `cout << ` `"Sum = "` `<< sum; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 30

**Time Complexity**: O(N)

## Recommended Posts:

- Product of all nodes in a doubly linked list divisible by a given number K
- Delete all nodes from the doubly linked list which are divisible by K
- Rotate Doubly linked list by N nodes
- Delete all the even nodes from a Doubly Linked List
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all the nodes from the doubly linked list that are greater than a given value
- Product of all prime nodes in a Doubly Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Large number arithmetic using doubly linked list
- Delete all the nodes from the list which are divisible by any given number K
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1

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.