Given a doubly linked list containing N nodes and given a number K. The task is to find the product of all such nodes which are divisible by K.
Input : List = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17 K = 3 Output : Product = 810 Input : List = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9 K = 2 Output : Product = 384
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 multiply that node’s value with the product so far and continue this process while the end of the list is not reached.
Below is the implementation of the above approach:
Product = 810
Time Complexity: O(N), where N is the number of nodes.
- Sum 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
- Product of all prime nodes in a Doubly Linked List
- 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
- Rotate Doubly linked list by N nodes
- Delete all the even nodes from 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
- Find pairs with given product in a sorted Doubly Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- 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.