-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path445.cpp
More file actions
41 lines (41 loc) · 1.12 KB
/
Copy path445.cpp
File metadata and controls
41 lines (41 loc) · 1.12 KB
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
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
stack<int> s1, s2, s;
ListNode *temp1 = l1, *temp2 = l2;
while (temp1) {
s1.push(temp1->val);
temp1 = temp1->next;
}
while (temp2) {
s2.push(temp2->val);
temp2 = temp2->next;
}
auto size1 = s1.size(), size2 = s2.size();
ListNode *l = size1 > size2 ? l1 : l2;
int cur = 0, nex = 0;
while (!s1.empty() || !s2.empty()) {
cur = nex;
if (!s1.empty()) cur += s1.top();
if (!s2.empty()) cur += s2.top();
nex = cur / 10;
cur = cur % 10;
s.push(cur);
if (!s1.empty()) s1.pop();
if (!s2.empty()) s2.pop();
}
if (nex == 1) {
s.push(1);
ListNode *temp = new ListNode(1);
temp->next = l;
l = temp;
}
ListNode *curr = l;
while (!s.empty()) {
curr->val = s.top();
s.pop();
curr = curr->next;
}
return l;
}
};