2130. Maximum Twin Sum of a Linked List
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 81% Topics: Linked List, Two Pointers, Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def pairSum(self, head):
"""
:type head: Optional[ListNode]
:rtype: int
"""
def reverseList(head):
dummy = ListNode()
while head:
dummy.next, head.next, head = head, dummy.next, head.next
return dummy.next
dummy = ListNode(next=head)
slow = fast = dummy
while fast.next and fast.next.next:
slow, fast = slow.next, fast.next.next
result = 0
head2 = reverseList(slow)
while head:
result = max(result, head.val+head2.val)
head, head2 = head.next, head2.next
return result
Solution from kamyu104/LeetCode-Solutions · MIT