mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 09:59:41 +01:00
28 lines
465 B
Go
28 lines
465 B
Go
|
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
|
||
|
}
|