⚡Flashcard Drill➔
NeetCode #320LC-2130MediumLinked List
← Back to All Problems

#320 · #2130 · Maximum Twin Sum of a Linked List(链表最大孪生和)

📌 Problem Statement & Constraints

In a linked list of even length n, the twin of node i is node n - 1 - i. The twin sum is the sum of a node and its twin. Return the maximum twin sum. Constraints: 2 <= n <= 10^5, n is even, 1 <= Node.val <= 10^5.

💡 Core Algorithmic Approaches

  1. Find the middle with slow/fast pointers, then reverse the second half.
  2. Now the twin pairs are aligned: the first node of the first half and the first node of the reversed second half are twins.
  3. Walk both halves together and take the maximum sum.
  4. This is O(n) time with O(1) extra space, which is the expected optimisation over copying values into an array.

💻 Benchmark Python3 Implementation

class Solution:
    def pairSum(self, head: Optional[ListNode]) -> int:
        # 1) find the middle (slow lands on the first node of the second half)
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # 2) reverse the second half
        prev = None
        cur = slow
        while cur:
            cur.next, prev, cur = prev, cur, cur.next
        # 3) walk both halves in lockstep
        a, b = head, prev
        best = 0
        while b:
            best = max(best, a.val + b.val)
            a = a.next
            b = b.next
        return best

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): three linear passes.
💾 Space Complexity
O(1): only pointers.

⚠️ Interview Pitfalls & Follow-ups

  • Copying values into an array: correct and simple, but O(n) space.
  • Reversing the first half instead: either half works, but the twin alignment requires exactly one reversal.
  • Stopping the loop on a: the reversed half is the shorter one for the middle node, so b drives the loop.
  • Forgetting that n is even: with an even length the halves are equal, so the pairing is exact.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Linked List Problems

中文速览与面试要点

链表最大孪生和

在一个大小为 n 且 n 为 偶数 的链表中,对于 0 &lt;= i &lt;= (n / 2) - 1 的 i,第 i 个节点(下标从 0 开始)的孪生节点为第 (n−1−i) 个节点。孪生和定义为两个节点的值的和。给你一个长度为偶数的链表的头节点 head,请你返回链表的 最大孪生和 。约束:链表的节点数目是 [2, 10^5] 中的 偶数 ;1 ≤ Node.val ≤ 10^5。

核心要点

  • 快慢指针 + 反转后半(O(1) 空间,标准解) :① 找中点(slow 停在后半的第一个节点);② 反转后半;③ 前半与反转后的后半同步遍历,累加并取最大值。
  • 为什么反转后能直接配对 :反转后后半的遍历顺序恰好是「从尾到头」,与前半的「从头到中」一一对应,即孪生关系。
  • 替代方案 :把链表转成数组后用双指针,O(n) 空间。
  • 复杂度 :三次线性遍历,O(n) 时间、O(1) 空间。

常见陷阱

  • 用「先求长度再取第 n−1−i 个」的双重循环 :每次取孪生节点需要 O(n) 遍历,整体 O(n²) 超时。
  • 快慢指针条件写成 while fast.next and fast.next.next :会让 slow 停在前半的最后一个节点,导致后半多一个节点,配对错位。
  • 比较循环以 p1 为终止条件 :前半比后半长(或等长),用 p1 会访问到已反转的节点,得到错误结果。应以 p2 为终止条件。
  • 初始值设为 0 :Node.val ≥ 1,初值 0 安全;但更严谨的写法是初始化为第一个孪生和。

参考实现

class Solution:
    def pairSum(self, head: 'ListNode') -> int:
        # ① 快慢指针找中点(slow 停在后半的第一个节点)
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        # ② 反转后半部分
        prev = None
        cur = slow
        while cur:
            nxt = cur.next
            cur.next = prev
            prev = cur
            cur = nxt

        # ③ 前半与反转后的后半同步遍历,求最大孪生和
        best = 0
        p1, p2 = head, prev
        while p2:
            s = p1.val + p2.val
            if s > best:
                best = s
            p1 = p1.next
            p2 = p2.next
        return best