-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
43 lines (40 loc) · 1010 Bytes
/
Copy path2.cpp
File metadata and controls
43 lines (40 loc) · 1010 Bytes
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
/**
* I did a trick. "if l1 or l2 ends, then ret list use the other one's last node
* as return list.
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *ret = nullptr, *now = nullptr, *prev = nullptr;
while (l1 != nullptr && l2 != nullptr) {
now = new ListNode(l1->val + l2->val);
prev ? prev->next = now : 0;
ret == nullptr ? ret = now : 0;
prev = now;
l1 = l1->next;
l2 = l2->next;
}
// only one list not end, or both end.
l1 == nullptr ? prev->next = l2 : prev->next = l1;
checkCarry(ret);
return ret;
}
void checkCarry(ListNode* l) {
ListNode *now = l, *prev = nullptr;
int carry = 0;
while (now) {
now->val += carry;
carry = now->val / 10;
now->val %= 10;
prev = now;
now = now->next;
}
while (carry) {
now = new ListNode(carry % 10);
prev->next = now;
carry /= 10;
prev = now;
now = now->next;
}
}
};