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)
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete linked list nodes which have a greater value on left side
- Address Calculation Sort using Hashing
- Delete every Kth node from circular linked list
- Delete all the nodes from the list which are less than K
- Iterative Merge Sort for Linked List
- Recursively Reversing a linked list (A simple implementation)
- Sum of the nodes of a Singly Linked List
- Segregate even and odd nodes in a Linked List using Deque
- Insertion at Specific Position in a Circular Doubly Linked List
- Reverse a doubly circular linked list
- Recursive Approach to find nth node from the end in the linked list
- Implementing a Linked List in Java using Class
- Find kth node from Middle towards Head of a Linked List
- Count pairs in a binary tree whose sum is equal to a given value x
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.