本文主要是介绍Leetcode 2130. Maximum Twin Sum of a Linked List [Python],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
朴素办法,记录全部值,双指针,头尾位置相加,并更新全局最大值
# 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 = headwhile i:container.append(i.val)i = i.nextstart = 0end = len(container)-1res = float('-inf')while start + 1!= end:temp = container[start] + container[end]res = max(res, temp)start += 1end -= 1temp = container[start] + container[end]res = max(res, temp)return res
这篇关于Leetcode 2130. Maximum Twin Sum of a Linked List [Python]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!