Skip to content

Commit

Permalink
Merge pull request #3394 from drxlx/2130-maximum-twin-sum-of-a-linked…
Browse files Browse the repository at this point in the history
…-list
  • Loading branch information
felivalencia3 authored Apr 14, 2024
2 parents 36da36e + 8ebf336 commit 6880ead
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions swift/2130-maximum-twin-sum-of-a-linked-list.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Question Link: https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/
*/

/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func pairSum(_ head: ListNode?) -> Int {
var slow = head
var fast = head
var prev: ListNode?
while fast != nil && fast?.next != nil {
fast = fast?.next?.next
var tmp = slow?.next
slow?.next = prev
prev = slow
slow = tmp
}
var res = 0
while slow != nil {
res = max(res, prev!.val + slow!.val)
prev = prev?.next
slow = slow?.next
}
return res
}
}

0 comments on commit 6880ead

Please sign in to comment.