朴素办法,记录全部值,双指针,头尾位置相加,并更新全局最大值
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def pairSum(self, head: Optional[ListNode]) -> int:
container = []
i = head
while i:
container.append(i.val)
i = i.next
start = 0
end = len(container)-1
res = float('-inf')
while start + 1!= end:
temp = container[start] + container[end]
res = max(res, temp)
start += 1
end -= 1
temp = container[start] + container[end]
res = max(res, temp)
return res
这篇文章讲解了如何通过朴素方法和双指针技巧高效地求解链表中两节点值之和的最大组合,通过遍历链表并逐步调整头尾指针位置来找到全局最大和。

529

被折叠的 条评论
为什么被折叠?



