Sum of all nodes in a doubly linked list divisible by a given number K
Last Updated :
22 Mar, 2023
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.
Examples:
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.
Algorithm:
- To represent a doubly linked list, create a DLL class.
- Create the static method push() to add a new node to the list’s top.
- Using the provided data, create a new node.
- Set the new node’s next pointer to the list’s current head.
- Set the previous node’s reference to null.
- Set the previous pointer of the current head, if it is not null, to the new node.
- Position the head such that it faces the new node.
- Give the head back.
- Create a static function called sumOfNode() to add up all the doubly linked list nodes that are divided by K.
- Set a variable’s sum value to 0.
- Use a while loop to iterate through the list until the end is reached (node is null).
- If the current node’s data is divisible by K, include it in the sum.
- Go to the following node in the list.
- Return the total.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int sumOfNode(Node** head_ref, int K)
{
Node* ptr = *head_ref;
Node* next;
int sum = 0;
while (ptr != NULL) {
next = ptr->next;
if (ptr->data % K == 0)
sum += ptr->data;
ptr = next;
}
return sum;
}
int main()
{
Node* head = NULL;
push(&head, 17);
push(&head, 7);
push(&head, 6);
push(&head, 9);
push(&head, 10);
push(&head, 16);
push(&head, 15);
int sum = sumOfNode(&head, 3);
cout << "Sum = " << sum;
}
|
Java
class Node {
int data;
Node next, prev;
Node( int d) {
data = d;
next = null ;
prev = null ;
}
}
class DLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
static int sumOfNode(Node node, int K) {
int sum = 0 ;
while (node != null ) {
if (node.data % K == 0 )
sum += node.data;
node = node.next;
}
return sum;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 17 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 9 );
head = push(head, 10 );
head = push(head, 16 );
head = push(head, 15 );
int sum = sumOfNode(head, 3 );
System.out.println( "Sum = " + sum);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node.prev = None
new_node. next = (head_ref)
if ((head_ref) ! = None ):
(head_ref).prev = new_node
(head_ref) = new_node
return head_ref
def sumOfNode(head_ref, K):
ptr = head_ref
next = None
sum = 0
while (ptr ! = None ) :
next = ptr. next
if (ptr.data % K = = 0 ):
sum + = ptr.data
ptr = next
return sum
if __name__ = = "__main__" :
head = None
head = push(head, 17 )
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 9 )
head = push(head, 10 )
head = push(head, 16 )
head = push(head, 15 )
sum = sumOfNode(head, 3 )
print ( "Sum =" , sum )
|
C#
using System;
public class Node
{
public int data;
public Node next, prev;
public Node( int d)
{
data = d;
next = null ;
prev = null ;
}
}
class DLL
{
static Node push(Node head, int data)
{
Node newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
static int sumOfNode(Node node, int K)
{
int sum = 0;
while (node != null )
{
if (node.data % K == 0)
sum += node.data;
node = node.next;
}
return sum;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
int sum = sumOfNode(head, 3);
Console.WriteLine( "Sum = " + sum);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .prev = null ;
this .next = null ;
}
}
function push(head , data) {
var newNode = new Node(data);
newNode.next = head;
newNode.prev = null ;
if (head != null )
head.prev = newNode;
head = newNode;
return head;
}
function sumOfNode(node , K) {
var sum = 0;
while (node != null ) {
if (node.data % K == 0)
sum += node.data;
node = node.next;
}
return sum;
}
var head = null ;
head = push(head, 17);
head = push(head, 7);
head = push(head, 6);
head = push(head, 9);
head = push(head, 10);
head = push(head, 16);
head = push(head, 15);
var sum = sumOfNode(head, 3);
document.write( "Sum = " + sum);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Complexity: O(1) because using constant variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...