Skip to content

Commit

Permalink
A faster solution for 2.add two numbsers
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnMa16 committed May 2, 2019
1 parent b053830 commit 0f4b28c
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Math/AddTwoNumbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@

class AddTwoNumbers {
func addTwoNumbers(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var carry = 0, l1 = l1, l2 = l2
let dummy = ListNode(0)
var node = dummy
guard let l1 = l1 else {return l2}
guard let l2 = l2 else {return l1}

while l1 != nil || l2 != nil || carry != 0 {
if l1 != nil {
carry += l1!.val
l1 = l1!.next
}
if l2 != nil {
carry += l2!.val
l2 = l2!.next
}

node.next = ListNode(carry % 10)
node = node.next!

carry = carry / 10
let outputNode = ListNode((l1.val + l2.val)%10)
if l1.val + l2.val > 9 {
outputNode.next = addTwoNumbers(addTwoNumbers(l1.next, l2.next),
ListNode(1))
} else {
outputNode.next = addTwoNumbers(l1.next, l2.next)
}

return dummy.next

return outputNode
}
}

0 comments on commit 0f4b28c

Please sign in to comment.