Maximum sum contiguous nodes in the given linked list
Last Updated :
09 Jun, 2021
Given a linked list, the task is to find the maximum sum for any contiguous nodes.
Examples:
Input: -2 -> -3 -> 4 -> -1 -> -2 -> 1 -> 5 -> -3 -> NULL
Output: 7
4 -> -1 -> -2 -> 1 -> 5 is the sub-list with the given sum.
Input: 1 -> 2 -> 3 -> 4 -> NULL
Output: 10
Approach: Kadane’s algorithm has been discussed in this article to work on arrays to find the maximum sub-array sum but it can be modified to work on linked lists too. Since Kadane’s algorithm doesn’t require to access random elements, it is also applicable on the linked lists in linear time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return ;
}
int MaxContiguousNodeSum(Node* head)
{
if (head == NULL)
return 0;
if (head->next == NULL)
return head->data;
int max_ending_here = head->data;
int max_so_far = head->data;
head = head->next;
while (head != NULL) {
max_ending_here = max(head->data,
max_ending_here + head->data);
max_so_far = max(max_ending_here, max_so_far);
head = head->next;
}
return max_so_far;
}
int main()
{
Node* head = NULL;
append(&head, -2);
append(&head, -3);
append(&head, 4);
append(&head, -1);
append(&head, -2);
append(&head, 1);
append(&head, 5);
append(&head, -3);
cout << MaxContiguousNodeSum(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node append(Node head_ref, int new_data)
{
Node new_node = new Node();
Node last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return head_ref;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return head_ref;
}
static int MaxContiguousNodeSum(Node head)
{
if (head == null )
return 0 ;
if (head.next == null )
return head.data;
int max_ending_here = head.data;
int max_so_far = head.data;
head = head.next;
while (head != null )
{
max_ending_here = Math.max(head.data,
max_ending_here + head.data);
max_so_far = Math.max(max_ending_here, max_so_far);
head = head.next;
}
return max_so_far;
}
public static void main(String[] args)
{
Node head = null ;
head = append(head, - 2 );
head = append(head, - 3 );
head = append(head, 4 );
head = append(head, - 1 );
head = append(head, - 2 );
head = append(head, 1 );
head = append(head, 5 );
head = append(head, - 3 );
System.out.print(MaxContiguousNodeSum(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def append(head_ref, new_data):
new_node = Node(new_data)
last = head_ref
new_node. next = None
if (head_ref = = None ):
head_ref = new_node
return head_ref
while (last. next ! = None ):
last = last. next
last. next = new_node
return head_ref
def MaxContiguousNodeSum(head):
if (head = = None ):
return 0
if (head. next = = None ):
return head.data
max_ending_here = head.data
max_so_far = head.data
head = head. next
while (head ! = None ):
max_ending_here = max (head.data,
max_ending_here +
head.data)
max_so_far = max (max_ending_here,
max_so_far)
head = head. next
return max_so_far
if __name__ = = '__main__' :
head = None
head = append(head, - 2 )
head = append(head, - 3 )
head = append(head, 4 )
head = append(head, - 1 )
head = append(head, - 2 )
head = append(head, 1 )
head = append(head, 5 )
head = append(head, - 3 )
print (MaxContiguousNodeSum(head))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node append(Node head_ref, int new_data)
{
Node new_node = new Node();
Node last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return head_ref;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return head_ref;
}
static int MaxContiguousNodeSum(Node head)
{
if (head == null )
return 0;
if (head.next == null )
return head.data;
int max_ending_here = head.data;
int max_so_far = head.data;
head = head.next;
while (head != null )
{
max_ending_here = Math.Max(head.data,
max_ending_here +
head.data);
max_so_far = Math.Max(max_ending_here,
max_so_far);
head = head.next;
}
return max_so_far;
}
public static void Main(String[] args)
{
Node head = null ;
head = append(head, -2);
head = append(head, -3);
head = append(head, 4);
head = append(head, -1);
head = append(head, -2);
head = append(head, 1);
head = append(head, 5);
head = append(head, -3);
Console.Write(MaxContiguousNodeSum(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function append( head_ref, new_data)
{
var new_node = new Node();
var last = head_ref;
new_node.data = new_data;
new_node.next = null ;
if (head_ref == null )
{
head_ref = new_node;
return head_ref;
}
while (last.next != null )
last = last.next;
last.next = new_node;
return head_ref;
}
function MaxContiguousNodeSum( head)
{
if (head == null )
return 0;
if (head.next == null )
return head.data;
let max_ending_here = head.data;
let max_so_far = head.data;
head = head.next;
while (head != null )
{
max_ending_here = Math.max(head.data,
max_ending_here + head.data);
max_so_far = Math.max(max_ending_here, max_so_far);
head = head.next;
}
return max_so_far;
}
var head = null ;
head = append(head, -2);
head = append(head, -3);
head = append(head, 4);
head = append(head, -1);
head = append(head, -2);
head = append(head, 1);
head = append(head, 5);
head = append(head, -3);
document.write(MaxContiguousNodeSum(head));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...