-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0002.add_two_numbers.cpp
107 lines (90 loc) · 2.34 KB
/
0002.add_two_numbers.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
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
#include <iostream>
#include <vector>
#include "leetcode.h"
using std::vector;
ListNode* add_two_numbers(ListNode *l1, ListNode *l2)
{
ListNode *dummy = new ListNode(-1, nullptr);
ListNode *cur = dummy;
int sum = 0;
int carry = 0;
while (l1 != nullptr && l2 != nullptr) {
sum = l1->val + l2->val + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum, nullptr);
cur = cur->next;
l1 = l1->next;
l2 = l2->next;
}
ListNode **res = (l1 != nullptr ? &l1 : &l2);
while (*res != nullptr) {
sum = (*res)->val + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum, nullptr);
cur = cur->next;
*res = (*res)->next;
}
if (l1 == l2 && carry) {
cur->next = new ListNode(carry, nullptr);
}
return dummy->next;
}
ListNode* _add_two_numbers(ListNode *l1, ListNode *l2)
{
ListNode *dummy = new ListNode(-1, nullptr);
ListNode *cur = dummy;
int sum = 0;
int carry = 0;
while (l1 != nullptr && l2 != nullptr) {
sum = l1->val + l2->val + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum, nullptr);
cur = cur->next;
l1 = l1->next;
l2 = l2->next;
}
while (l1 != nullptr) {
sum = l1->val + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum, nullptr);
cur = cur->next;
l1 = l1->next;
}
while (l2 != nullptr) {
sum = l2->val + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum, nullptr);
cur = cur->next;
l2 = l2->next;
}
if (carry != 0) {
cur->next = new ListNode(carry, nullptr);
}
return dummy->next;
}
int main () {
#ifdef LOCAL
freopen("0002.in", "r", stdin);
#endif
int m = 0, n = 0;
while (std::cin >> m >> n) {
vector<int> nums1(m, 0);
vector<int> nums2(n, 0);
for (int i = 0; i < m; ++i) {
std::cin >> nums1[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> nums2[i];
}
ListNode *l1 = build_list(nums1);
ListNode *l2 = build_list(nums2);
ListNode *res = add_two_numbers(l1, l2);
show_list(res);
}
return 0;
}