Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7360ef

Browse files
raof01azl397985856
authored andcommitted
feat: azl397985856#445: add C++ implementation (azl397985856#70)
1 parent 42701df commit e7360ef

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

‎problems/445.add-two-numbers-ii.md‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Output: 7 -> 8 -> 0 -> 7
2525

2626
最后根据stack生成最终的链表即可。
2727

28+
> 也可以先将两个链表逆置,然后相加,最后将结果再次逆置。
29+
2830
## 关键点解析
2931

3032
- 栈的基本操作
@@ -33,6 +35,10 @@ Output: 7 -> 8 -> 0 -> 7
3335
- 注意特殊情况, 比如 1 + 99 = 100
3436

3537
## 代码
38+
* 语言支持:JS,C++
39+
40+
JavaScript Code:
41+
3642
```js
3743
/*
3844
* @lc app=leetcode id=445 lang=javascript
@@ -135,3 +141,97 @@ var addTwoNumbers = function(l1, l2) {
135141
};
136142

137143
```
144+
C++ Code:
145+
```C++
146+
/**
147+
* Definition for singly-linked list.
148+
* struct ListNode {
149+
* int val;
150+
* ListNode *next;
151+
* ListNode(int x) : val(x), next(NULL) {}
152+
* };
153+
*/
154+
class Solution {
155+
public:
156+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
157+
auto carry = 0;
158+
auto ret = (ListNode*)nullptr;
159+
auto s1 = vector<int>();
160+
toStack(l1, s1);
161+
auto s2 = vector<int>();
162+
toStack(l2, s2);
163+
while (!s1.empty() || !s2.empty() || carry != 0) {
164+
auto v1 = 0;
165+
auto v2 = 0;
166+
if (!s1.empty()) {
167+
v1 = s1.back();
168+
s1.pop_back();
169+
}
170+
if (!s2.empty()) {
171+
v2 = s2.back();
172+
s2.pop_back();
173+
}
174+
auto v = v1 + v2 + carry;
175+
carry = v / 10;
176+
auto tmp = new ListNode(v % 10);
177+
tmp->next = ret;
178+
ret = tmp;
179+
}
180+
return ret;
181+
}
182+
private:
183+
// 此处若返回而非传入vector,跑完所有测试用例多花8ms
184+
void toStack(const ListNode* l, vector<int>& ret) {
185+
while (l != nullptr) {
186+
ret.push_back(l->val);
187+
l = l->next;
188+
}
189+
}
190+
};
191+
192+
// 逆置,相加,再逆置。跑完所有测试用例比第一种解法少花4ms
193+
class Solution {
194+
public:
195+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
196+
auto rl1 = reverseList(l1);
197+
auto rl2 = reverseList(l2);
198+
auto ret = add(rl1, rl2);
199+
return reverseList(ret);
200+
}
201+
private:
202+
ListNode* reverseList(ListNode* head) {
203+
ListNode* prev = NULL;
204+
ListNode* cur = head;
205+
ListNode* next = NULL;
206+
while (cur != NULL) {
207+
next = cur->next;
208+
cur->next = prev;
209+
prev = cur;
210+
cur = next;
211+
}
212+
return prev;
213+
}
214+
215+
ListNode* add(ListNode* l1, ListNode* l2) {
216+
ListNode* ret = nullptr;
217+
ListNode* cur = nullptr;
218+
int carry = 0;
219+
while (l1 != nullptr || l2 != nullptr || carry != 0) {
220+
carry += (l1 == nullptr ? 0 : l1->val) + (l2 == nullptr ? 0 : l2->val);
221+
auto temp = new ListNode(carry % 10);
222+
carry /= 10;
223+
if (ret == nullptr) {
224+
ret = temp;
225+
cur = ret;
226+
}
227+
else {
228+
cur->next = temp;
229+
cur = cur->next;
230+
}
231+
l1 = l1 == nullptr ? nullptr : l1->next;
232+
l2 = l2 == nullptr ? nullptr : l2->next;
233+
}
234+
return ret;
235+
}
236+
};
237+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /