利用动态规划的思想,最大子链表的和只与之前子链表+当前值 和 当前值有关,并用一个全局值记录整个过程中出现的最大值
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return int整型
#
class Solution:
def FindGreatestSumOfSubArray(self , head: ListNode) -> int:
# write code here
if not head: return 0
if not head.next:return head.val
res = cur = float("-inf")
while head:
cur = max(cur + head.val, head.val)
res = max(res, cur)
head = head.next
return res