forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTwoNumbers.swift
33 lines (29 loc) · 967 Bytes
/
AddTwoNumbers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Question Link: https://leetcode.com/problems/add-two-numbers/
* Primary idea: use carry and iterate through both linked lists
* Time Complexity: O(n), Space Complexity: O(1)
*
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/
class AddTwoNumbers {
func addTwoNumbers(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
guard let l1 = l1 else {return l2}
guard let l2 = l2 else {return l1}
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 outputNode
}
}