1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

go: add «2130. Maximum Twin Sum of a Linked List»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-25 21:37:56 +02:00
parent 8ab17a79b2
commit 4164be15ac
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,27 @@
package main
func pairSum(head *ListNode) int {
findMidAndBuild := func() ([]int, *ListNode) {
stack := make([]int, 0)
slow, fast := head, head
for fast != nil {
stack = append(stack, slow.Val)
slow, fast = slow.Next, fast.Next.Next
}
return stack, slow
}
stack, node := findMidAndBuild()
foundMax, idx := 0, len(stack)-1
for node != nil {
foundMax = max(foundMax, stack[idx]+node.Val)
node = node.Next
idx--
}
return foundMax
}