-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_Two_Numbers.swift
151 lines (127 loc) · 3.61 KB
/
Add_Two_Numbers.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
*/
/*
Solution:
use temp to help recording the sum result, if temp > 0, next one should add 1
Time Complexity: O(max(M,N)), go through 2 list
Space Complexity: O(max(M,N)) most largest length would be max + 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 Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var head = ListNode(0)
var node = head
var temp = 0
var l1 = l1
var l2 = l2
while l1 != nil, l2 != nil {
node.next = ListNode((temp + l1!.val + l2!.val) % 10)
temp = (temp + l1!.val + l2!.val) / 10
node = node.next!
l1 = l1!.next
l2 = l2!.next
}
if l1 == nil {
while l2 != nil {
node.next = ListNode((temp + l2!.val) % 10)
temp = (temp + l2!.val ?? 0) / 10
node = node.next!
l2 = l2!.next
}
}
if l2 == nil {
while l1 != nil {
node.next = ListNode((temp + l1!.val) % 10)
temp = (temp + l1!.val) / 10
node = node.next!
l1 = l1!.next
}
}
if temp > 0 {
node.next = ListNode(temp)
}
return head.next
}
}
Optimize Solution 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 Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var head = ListNode(0)
var node = head
var temp = 0
var l1 = l1
var l2 = l2
while l1 != nil || l2 != nil {
let x1 = (l1 == nil ? 0 : l1!.val)
let x2 = (l2 == nil ? 0 : l2!.val)
node.next = ListNode((temp + x1 + x2) % 10)
temp = (temp + x1 + x2) / 10
node = node.next!
if l1 != nil { l1 = l1!.next}
if l2 != nil { l2 = l2!.next}
}
if temp > 0 {
node.next = ListNode(temp)
}
return head.next
}
}
/**
* 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 Solution {
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var head = ListNode(0)
var l1 = l1
var l2 = l2
var temp = 0
var node = head
while l1 != nil || l2 != nil {
temp = (l1?.val ?? 0) + (l2?.val ?? 0) + temp
node.next = ListNode(temp%10)
temp = temp/10
if l1 != nil { l1 = l1!.next }
if l2 != nil { l2 = l2!.next }
node = node.next!
}
if temp != 0 {
node.next = ListNode(temp)
}
return head.next
}
}