-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0445.cpp
135 lines (119 loc) · 2.64 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
128
129
130
131
132
133
134
135
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// return func1(l1, l2);
return func2(l1, l2);
}
ListNode *func1(ListNode *l1, ListNode *l2) {
// l1 = listReverse(l1);
// l2 = listReverse(l2);
l1 = listNonRecursiveReverse(l1);
l2 = listNonRecursiveReverse(l2);
ListNode *head = NULL;
ListNode *curr = NULL;
int carry = 0;
while (l1 != NULL || l2 != NULL || carry > 0) {
int val = carry;
if (l1) {
val += l1->val;
l1 = l1->next;
}
if (l2) {
val += l2->val;
l2 = l2->next;
}
carry = val / 10;
if (curr == NULL) {
curr = new ListNode(val % 10);
head = curr;
} else {
curr->next = new ListNode(val % 10);
curr = curr->next;
}
}
return listReverse(head);
}
// stack, 2020-03-05
ListNode *func2(ListNode *l1, ListNode *l2) {
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
stack<ListNode *> stk1;
stack<ListNode *> stk2;
while (l1 != NULL) {
stk1.push(l1);
l1 = l1->next;
}
while (l2 != NULL) {
stk2.push(l2);
l2 = l2->next;
}
ListNode *head = NULL;
ListNode *curr = NULL;
int carry = 0;
while (!stk1.empty() || !stk2.empty()) {
int sum = carry;
if (!stk1.empty()) {
sum += stk1.top()->val;
stk1.pop();
}
if (!stk2.empty()) {
sum += stk2.top()->val;
stk2.pop();
}
curr = new ListNode(sum % 10);
carry = sum / 10;
if (head == NULL) {
head = curr;
} else {
curr->next = head;
head = curr;
}
}
if (carry > 0) {
curr = new ListNode(carry);
curr->next = head;
head = curr;
}
return head;
}
private:
// ** recursive
ListNode *listReverse(ListNode *node) {
if (node == NULL || node->next == NULL) {
return node;
}
ListNode *second = node->next;
ListNode *newHead = listReverse(second);
second->next = node;
node->next = NULL;
return newHead;
}
// ** non-recursive
ListNode *listNonRecursiveReverse(ListNode *node) {
if (node == NULL) {
return NULL;
}
vector<ListNode *> vec;
while (node != NULL) {
vec.push_back(node);
node = node->next;
}
for (int i = vec.size() - 1; i > 0; i--) {
vec[i]->next = vec[i - 1];
}
vec[0]->next = NULL;
return vec[vec.size() - 1];
}
};