Skip to content

Commit 2a77937

Browse files
authored
Create 2130-maximum-twin-sum-of-a-linked-list.py
1 parent 4742bf3 commit 2a77937

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def pairSum(self, head: Optional[ListNode]) -> int:
3+
slow, fast = head, head
4+
prev = None
5+
while fast and fast.next:
6+
fast = fast.next.next
7+
tmp = slow.next
8+
slow.next = prev
9+
prev = slow
10+
slow = tmp
11+
12+
res = 0
13+
while slow:
14+
res = max(res, prev.val + slow.val)
15+
prev = prev.next
16+
slow = slow.next
17+
return res

0 commit comments

Comments
 (0)