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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 a doubly linked list that are smaller than a given value
- Delete all Prime Nodes from a Doubly Linked List
- Remove all the Even Digit Sum Nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Replace even nodes of a doubly linked list with the elements of array
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Find pairs with given product in a sorted Doubly Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- 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
- Find the product of last N nodes of the given Linked List
- Find the product of first k nodes of the given Linked List