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.
Input: List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 K = 3 Output: Sum = 30 Input: List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9 K = 2 Output: 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:
Sum = 30
Time Complexity: O(N)
- 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
- Delete all the even nodes from a Doubly Linked List
- Rotate Doubly linked list by N nodes
- Product of all prime nodes in a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all Prime Nodes from a Doubly Linked List
- Delete all the nodes from a doubly linked list that are smaller than a given value
- 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
- Number of connected components in a 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 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 firstname.lastname@example.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.