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
4 -> -1 -> -2 -> 1 -> 5 is the sub-list with the given sum.
Input: 1 -> 2 -> 3 -> 4 -> NULL
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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Maximum sum of K consecutive nodes in the given Linked List
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Delete N nodes after M nodes of a linked list
- Linked List Sum of Nodes Between 0s
- Find sum of even and odd nodes in a linked list
- Sum of all distinct nodes in a linked list
- Sum of the nodes of a Singly Linked List
- Sum of all odd frequency nodes of the Linked List
- Find the sum of last n nodes of the given Linked List
- Linked List Product of Nodes Between 0s
- Sum of the nodes of a Circular Linked List
- Segregate even and odd nodes in a Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Sum of the alternate nodes of linked list
- Segregate even and odd nodes in a Linked List using Deque
- Replace nodes with duplicates in linked list
- Find the product of first k nodes of the given Linked List
- Delete continuous nodes with sum K from a given linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.