forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0445.cpp
127 lines (120 loc) · 3.03 KB
/
0445.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <stack>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
// class Solution
// {
// public:
// ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
// {
// stack<int> stack1, stack2, result;
// ListNode* h = new ListNode(-1);
// ListNode*cur = h;
// while (l1 != nullptr)
// {
// stack1.push(l1->val);
// l1 = l1->next;
// }
// while (l2 != nullptr)
// {
// stack2.push(l2->val);
// l2 = l2->next;
// }
// int add = 0;
// while (!stack1.empty() || !stack2.empty() || add)
// {
// int top1 = 0;
// int top2 = 0;
// if (!stack1.empty())
// {
// top1 = stack1.top();
// stack1.pop();
// }
// if (!stack2.empty())
// {
// top2 = stack2.top();
// stack2.pop();
// }
// int val = top1 + top2 + add;
// add = val/10;
// result.push(val%10);
// }
// while (!result.empty())
// {
// cur->next = new ListNode(result.top());
// result.pop();
// cur = cur->next;
// }
// ListNode* retNode = h->next;
// delete h;
// return retNode;
// }
// };
class Solution {
void backtrack(ListNode* proc)
{
if (proc->next)
{
backtrack(proc->next);
if (proc->next->val >= 10)
{
proc->val += proc->next->val / 10;
proc->next->val %= 10;
}
}
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode *f1 = l1, *f2 = l2;
while (f1 && f2)
{
f1 = f1->next;
f2 = f2->next;
}
ListNode *dummy = new ListNode(0), *proc = dummy;
ListNode *s1 = l1, *s2 = l2;
while (s1 || s2)
{
if (f1)
{
proc->next = s1;
proc = proc->next;
s1 = s1->next;
f1 = f1->next;
} else if (f2)
{
proc->next = s2;
proc = proc->next;
s2 = s2->next;
f2 = f2->next;
} else
{
proc->next = s1;
proc->next->val += s2->val;
proc = proc->next;
s1 = s1->next;
s2 = s2->next;
}
}
backtrack(dummy);
if (dummy->val != 0)
return dummy;
else
{
ListNode* root = dummy->next;
delete dummy;
return root;
}
}
};
int main()
{
return 0;
}