2130. Maximum Twin Sum of a Linked List
Problem

Solution
The key requirements of this problem are as follows:
- The linked list has an even legnth.
- The
ith node is paired with the(length-1-i)th node. - Return the maximum sum among all twin pairs.
To satisfy these requirements, I used a two-pointer approach.
First, I split the problem into three steps.
- Find the middle nodes of the list.
- Reverse the second half of the list.
- Compare the two halves node by node and track the maximum sum.
To find the middle, I use slow and fast pointer.
slowstarts asheadand moves single step at a time.faststarts asheadand moves two steps at a time.
When fast reaches the end, slow points to the middle of the list.
To reverse the second half, initialize three pointers.
prevstarts as None.revstarts asslow.aheadis used to store the next node during iteration.
During reverse is like below.
- Store
rev.nextinahead. - Change
rev.nextto point toprev. - Move
prevtorev - Move
revtoahead.
Repeat until the second half is fully reversed.
After that, prev points to the head of the reversed half.
Now, simply compare the two halves.
In each iteration, add prev.val and head.val and update when result is smaller than the sum.
At the end of the loop, return the result.
class Solution(object):
def pairSum(self, head):
slow = fast = head
result = 0
while fast:
fast = fast.next.next
slow = slow.next
prev = None
rev = slow
while rev:
ahead = rev.next
rev.next = prev
prev = rev
rev = ahead
while prev:
temp = prev.val+head.val
if temp > result:
result = temp
prev = prev.next
head = head.next
return result
https://github.com/K-MarkLee/LeetCode-Practice
GitHub - K-MarkLee/LeetCode-Practice: A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2
A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2.0](https://github.com/maitreya2954/LeetHub-2.0-Firefox) - K-MarkLee/LeetCode-Practice
github.com
'코딩테스트 > LeetCode' 카테고리의 다른 글
| LeetCode 872. Leaf-Similar Trees (0) | 2026.01.24 |
|---|---|
| LeetCode 104. Maximum Depth of Binary Tree (0) | 2026.01.24 |
| LeetCode 206. Reverse Linked List (0) | 2026.01.22 |
| LeetCode 328. Odd Even Linked List (0) | 2026.01.21 |
| LeetCode 2095. Delete the Middle Node of a Linked List (0) | 2026.01.17 |