Maximum sum contiguous nodes in the given linked list

Given a linked list, the task is to find the maximum sum for any contiguous nodes.


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++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// A linked list node
class Node {
    int data;
    Node* next;
/* Given a reference (pointer to pointer) to the head 
of a list and an int, appends a new node at the end */
void append(Node** head_ref, int new_data)
    // Allocate node
    Node* new_node = new Node();
    Node* last = *head_ref; /* used in step 5*/
    // Put in the data
    new_node->data = new_data;
    /* This new node is going to be 
    the last node, so make next of 
    it as NULL*/
    new_node->next = NULL;
    /* If the Linked List is empty, 
    then make the new node as head */
    if (*head_ref == NULL) {
        *head_ref = new_node;
    // Else traverse till the last node
    while (last->next != NULL)
        last = last->next;
    // Change the next of last node
    last->next = new_node;
// Function to return the maximum contiguous
// nodes sum in the given linked list
int MaxContiguousNodeSum(Node* head)
    // If the list is empty
    if (head == NULL)
        return 0;
    // If the list contains a single element
    if (head->next == NULL)
        return head->data;
    // max_ending_here will store the maximum sum
    // ending at the current node, currently it
    // will be initialised to the maximum sum ending
    // at the first node which is the first node's value
    int max_ending_here = head->data;
    // max_so_far will store the maximum sum of
    // contiguous nodes so far which is the required
    // answer at the end of the linked list traversal
    int max_so_far = head->data;
    // Starting from the second node
    head = head->next;
    // While there are nodes in linked list
    while (head != NULL) {
        // max_ending_here will be the maximum of either
        // the current node's value or the current node's
        // value added with the max_ending_here
        // for the previous node
        max_ending_here = max(head->data,
                              max_ending_here + head->data);
        // Update the maximum sum so far
        max_so_far = max(max_ending_here, max_so_far);
        // Get to the next node
        head = head->next;
    // Return the maximum sum so far
    return max_so_far;
// Driver code
int main()
    // Create the linked list
    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;




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.