-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0002.cpp
35 lines (32 loc) · 790 Bytes
/
0002.cpp
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
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
vector<int> sum;
int carry = 0;
while (l1 || l2)
{
sum.push_back(carry);
if (l1)
{
sum.back() += l1->val;
l1 = l1->next;
}
if (l2)
{
sum.back() += l2->val;
l2 = l2->next;
}
carry = sum.back() / 10;
sum.back() %= 10;
}
if (carry > 0)
sum.push_back(carry);
ListNode* next = nullptr;
for (int i = sum.size() - 1; i >= 0; i--)
{
auto current = new ListNode(sum[i], next);
next = current;
}
return next;
}
};