Given a singly circular linked list. The task is to find the sum and product of nodes which are divisible by K of the given linked list.
Input : List = 5->6->7->8->9->10->11->11 K = 11 Output : Sum = 22, Product = 121 Input : List = 15->7->3->9->11->5 K = 5 Output : Product = 75, Sum = 20
- Initialize a pointer current with the head of the circular linked list and a sum variable sum with 0 and a product variable product with 1.
- Start traversing the linked list using a do while loop until all the nodes get traversed.
- If current node data is divisible by given key.
- Add the value of current node to the sum i.e. sum = sum + current -> data.
- Multiply the value of current node to the product i.e. product = product * current -> data.
- Increment the pointer to the next node of linked list i.e. temp = temp -> next.
- Print the sum and product.
Below is the implementation of the above approach:
Initial List: 5 6 7 8 9 10 11 11 Sum = 22, Product = 121
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 and Product of the nodes of a Singly Linked List which are divisible by K
- Convert singly linked list into circular linked list
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of all Fibonacci Nodes of a Singly Linked List
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Product of the nodes of a Singly Linked List
- Find minimum and maximum elements in singly Circular Linked List
- Difference between Singly linked list and Doubly linked list
- Circular Singly Linked List | Insertion
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Convert Singly Linked List to XOR Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Sum of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Java Program to Create a Singly Linked List and Count the Number of Nodes
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.