# Find Maximum sum of k consecutive nodes in linked list

Here given code implementation process.

``````// C Program
// Find Maximum sum of k consecutive nodes in linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

{
int data;
};
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
}
return sll;
}
// Returns a new Node of linked list
{
// Create dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void addNode(struct SingleLL *sll, int data)
{
{
}
else
{
// Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
void display(struct SingleLL *sll)
{
{
return;
}
while (temp != NULL)
{
{
printf(" →");
}
printf(" %d", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}
// Find the max sum of consecutive K nodes
void consecutiveKSum(struct SingleLL *sll, int k)
{
if (sll->head == NULL || k < 1)
{
return;
}
int result = 0;
int sum = 0;
int counter = 1;
while (auxiliary != NULL)
{
sum = sum + auxiliary->data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start->data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start->next;
}
}
// Visit to next node
auxiliary = auxiliary->next;
counter++;
}
if (counter <= k)
{
printf("\n %d Consecutive nodes are not exists", k);
}
else
{
// Display calculated result
printf(" Consecutive %d-nodes max sum is : %d\n", k, result);
}
}
int main()
{
// Create a empty linked list
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
display(sll);
int k = 4;
consecutiveKSum(sll, k);
return 0;
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````/*
Java Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
// Find the max sum of consecutive K nodes
public void consecutiveKSum(int k)
{
if (this.head == null || k < 1)
{
return;
}
int result = 0;
int sum = 0;
int counter = 1;
while (auxiliary != null)
{
sum = sum + auxiliary.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start.next;
}
}
// Visit to next node
auxiliary = auxiliary.next;
counter++;
}
if (counter <= k)
{
System.out.print("\n " + k + " Consecutive nodes are not exists");
}
else
{
// Display calculated result
System.out.print(" Consecutive " + k + "-nodes max sum is : " + result + "\n");
}
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
int k = 4;
sll.consecutiveKSum(k);
}
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};;
class SingleLL
{
SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
// Append the node at last position
temp->next = node;
}
}
void display()
{
{
cout << "\n Empty linked list\n";
return;
}
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
// Visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
// Find the max sum of consecutive K nodes
void consecutiveKSum(int k)
{
if (this->head == NULL || k < 1)
{
return;
}
int result = 0;
int sum = 0;
int counter = 1;
while (auxiliary != NULL)
{
sum = sum + auxiliary->data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start->data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start->next;
}
}
// Visit to next node
auxiliary = auxiliary->next;
counter++;
}
if (counter <= k)
{
cout << "\n " << k << " Consecutive nodes are not exists";
}
else
{
// Display calculated result
cout << " Consecutive " << k << "-nodes max sum is : " << result << "\n";
}
}
};
int main()
{
SingleLL sll = SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
int k = 4;
sll.consecutiveKSum(k);
return 0;
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````// Include namespace system
using System;
/*
C# Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
public int data;
{
this.data = data;
this.next = null;
}
};
public class SingleLL
{
public SingleLL()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
// Find the max sum of consecutive K nodes
public void consecutiveKSum(int k)
{
if (this.head == null || k < 1)
{
return;
}
int result = 0;
int sum = 0;
int counter = 1;
while (auxiliary != null)
{
sum = sum + auxiliary.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start.next;
}
}
// Visit to next node
auxiliary = auxiliary.next;
counter++;
}
if (counter <= k)
{
Console.Write("\n " + k + " Consecutive nodes are not exists");
}
else
{
// Display calculated result
Console.Write(" Consecutive " + k + "-nodes max sum is : " + result + "\n");
}
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
int k = 4;
sll.consecutiveKSum(k);
}
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````<?php
/*
Php Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
};
class SingleLL
{

function __construct()
{
}
{
{
}
else
{
//Find last node
while (\$temp->next != null)
{
\$temp = \$temp->next;
}
// Append the node at last position
\$temp->next = \$node;
}
}
public	function display()
{
{
return;
}
while (\$temp != null)
{
{
echo " →";
}
echo " ". \$temp->data;
// Visit to next node
\$temp = \$temp->next;
}
echo " → NULL\n";
}
// Find the max sum of consecutive K nodes
public	function consecutiveKSum(\$k)
{
if (\$this->head == null || \$k < 1)
{
return;
}
\$result = 0;
\$sum = 0;
\$counter = 1;
while (\$auxiliary != null)
{
\$sum = \$sum + \$auxiliary->data;
if (\$counter >= \$k)
{
if (\$counter == \$k)
{
// Get first pair of k nodes
\$result = \$sum;
}
else
{
\$sum = \$sum - \$start->data;
if (\$sum > \$result)
{
\$result = \$sum;
}
// Visit to next node
\$start = \$start->next;
}
}
// Visit to next node
\$auxiliary = \$auxiliary->next;
\$counter++;
}
if (\$counter <= \$k)
{
echo "\n ". \$k ." Consecutive nodes are not exists";
}
else
{
// Display calculated result
echo " Consecutive ". \$k ."-nodes max sum is : ". \$result ."\n";
}
}
}

function main()
{
\$sll = new SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
\$sll->display();
\$k = 4;
\$sll->consecutiveKSum(\$k);
}
main();``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````/*
Node Js Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
}
{
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
// Visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
// Find the max sum of consecutive K nodes
consecutiveKSum(k)
{
if (this.head == null || k < 1)
{
return;
}
var result = 0;
var sum = 0;
var counter = 1;
while (auxiliary != null)
{
sum = sum + auxiliary.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start.next;
}
}
// Visit to next node
auxiliary = auxiliary.next;
counter++;
}
if (counter <= k)
{
process.stdout.write("\n " + k + " Consecutive nodes are not exists");
}
else
{
// Display calculated result
process.stdout.write(" Consecutive " + k + "-nodes max sum is : " + result + "\n");
}
}
}

function main()
{
var sll = new SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
var k = 4;
sll.consecutiveKSum(k);
}
main();``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````#   Python 3 Program for
#   Maximum sum of K consecutive nodes in the given Linked List

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :

else :
# Find last node
while (temp.next != None) :
temp = temp.next

#  Append the node at last position
temp.next = node

def display(self) :
return

while (temp != None) :
print(" →", end = "")

print("", temp.data, end = "")
#  Visit to next node
temp = temp.next

print(" → NULL")

#  Find the max sum of consecutive K nodes
def consecutiveKSum(self, k) :
if (self.head == None or k < 1) :
return

result = 0
sum = 0
counter = 1
while (auxiliary != None) :
sum = sum + auxiliary.data
if (counter >= k) :
if (counter == k) :
#  Get first pair of k nodes
result = sum
else :
sum = sum - start.data
if (sum > result) :
result = sum

#  Visit to next node
start = start.next

#  Visit to next node
auxiliary = auxiliary.next
counter += 1

if (counter <= k) :
print("\n ", k ," Consecutive nodes are not exists", end = "")
else :
#  Display calculated result
print(" Consecutive ", k ,"-nodes max sum is : ", result )

def main() :
sll = SingleLL()
#   7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display()
k = 4
sll.consecutiveKSum(k)

if __name__ == "__main__": main()``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive  4 -nodes max sum is :  24``````
``````#   Ruby Program for
#   Maximum sum of K consecutive nodes in the given Linked List

attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL

def initialize()
end

else
# Find last node
while (temp.next != nil)
temp = temp.next
end

#  Append the node at last position
temp.next = node
end

end

def display()
return
end

while (temp != nil)
print(" →")
end

print(" ", temp.data)
#  Visit to next node
temp = temp.next
end

print(" → NULL\n")
end

#  Find the max sum of consecutive K nodes
def consecutiveKSum(k)
if (self.head == nil || k < 1)
return
end

result = 0
sum = 0
counter = 1
while (auxiliary != nil)
sum = sum + auxiliary.data
if (counter >= k)
if (counter == k)
#  Get first pair of k nodes
result = sum
else
sum = sum - start.data
if (sum > result)
result = sum
end

#  Visit to next node
start = start.next
end

end

#  Visit to next node
auxiliary = auxiliary.next
counter += 1
end

if (counter <= k)
print("\n ", k ," Consecutive nodes are not exists")
else
#  Display calculated result
print(" Consecutive ", k ,"-nodes max sum is : ", result ,"\n")
end

end

end

def main()
sll = SingleLL.new()
#   7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display()
k = 4
sll.consecutiveKSum(k)
end

main()``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24
``````
``````/*
Scala Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
def this(data: Int)
{
this(data, null);
}
};
{
def this()
{
this(null);
}
def addNode(data: Int): Unit = {
{
}
else
{
//Find last node
while (temp.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp.next = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Find the max sum of consecutive K nodes
def consecutiveKSum(k: Int): Unit = {
if (this.head == null || k < 1)
{
return;
}
var result: Int = 0;
var sum: Int = 0;
var counter: Int = 1;
while (auxiliary != null)
{
sum = sum + auxiliary.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start.next;
}
}
// Visit to next node
auxiliary = auxiliary.next;
counter += 1;
}
if (counter <= k)
{
print("\n " + k + " Consecutive nodes are not exists");
}
else
{
// Display calculated result
print(" Consecutive " + k + "-nodes max sum is : " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
var k: Int = 4;
sll.consecutiveKSum(k);
}
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````
``````/*
Swift 4 Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
};
class SingleLL
{
init()
{
}
{
{
}
else
{
//Find last node
while (temp!.next  != nil)
{
temp = temp!.next;
}
// Append the node at last position
temp!.next = node;
}
}
func display()
{
{
return;
}
while (temp  != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" → NULL");
}
// Find the max sum of consecutive K nodes
func consecutiveKSum(_ k: Int)
{
if (self.head == nil || k < 1)
{
return;
}
var result: Int = 0;
var sum: Int = 0;
var counter: Int = 1;
while (auxiliary  != nil)
{
sum = sum + auxiliary!.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start!.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start!.next;
}
}
// Visit to next node
auxiliary = auxiliary!.next;
counter += 1;
}
if (counter <= k)
{
print("\n", k ,"Consecutive nodes are not exists", terminator: "");
}
else
{
// Display calculated result
print(" Consecutive \(k)-nodes max sum is : ", result );
}
}
}
func main()
{
let sll: SingleLL = SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
let k: Int = 4;
sll.consecutiveKSum(k);
}
main();``````

#### Output

``````  7 →  2 →  3 →  4 →  8 →  6 →  -2 →  12 →  4 → NULL
Consecutive 4-nodes max sum is :  24``````
``````/*
Kotlin Program for
Maximum sum of K consecutive nodes in the given Linked List
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
};
class SingleLL
{
constructor()
{
}
{
{
}
else
{
//Find last node
while (temp?.next != null)
{
temp = temp.next;
}
// Append the node at last position
temp?.next = node;
}
}
fun display(): Unit
{
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
// Visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
// Find the max sum of consecutive K nodes
fun consecutiveKSum(k: Int): Unit
{
if (this.head == null || k < 1)
{
return;
}
var result: Int = 0;
var sum: Int = 0;
var counter: Int = 1;
while (auxiliary != null)
{
sum = sum + auxiliary.data;
if (counter >= k)
{
if (counter == k)
{
// Get first pair of k nodes
result = sum;
}
else
{
sum = sum - start!!.data;
if (sum > result)
{
result = sum;
}
// Visit to next node
start = start.next;
}
}
// Visit to next node
auxiliary = auxiliary.next;
counter += 1;
}
if (counter <= k)
{
print("\n " + k + " Consecutive nodes are not exists");
}
else
{
// Display calculated result
print(" Consecutive " + k + "-nodes max sum is : " + result + "\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var sll: SingleLL = SingleLL();
//  7 → -2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
sll.display();
var k: Int = 4;
sll.consecutiveKSum(k);
}``````

#### Output

`````` 7 → 2 → 3 → 4 → 8 → 6 → -2 → 12 → 4 → NULL
Consecutive 4-nodes max sum is : 24``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.