Skip to content

Commit c7e583d

Browse files
committed
feat: add solutions to lc/lcof2 problem: Add Two Numbers II
1 parent 6a28588 commit c7e583d

File tree

11 files changed

+289
-60
lines changed

11 files changed

+289
-60
lines changed

lcof2/剑指 Offer II 025. 链表中的两数相加/README.md

+53-12
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@
6767
```python
6868
# Definition for singly-linked list.
6969
# class ListNode:
70-
# def __init__(self, x):
71-
# self.val = x
72-
# self.next = None
73-
70+
# def __init__(self, val=0, next=None):
71+
# self.val = val
72+
# self.next = next
7473
class Solution:
7574
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
7675
s1, s2 = [], []
@@ -80,12 +79,11 @@ class Solution:
8079
while l2:
8180
s2.append(l2.val)
8281
l2 = l2.next
83-
carry, dummy = 0, ListNode(-1)
82+
carry, dummy = 0, ListNode()
8483
while s1 or s2 or carry:
8584
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
8685
# 创建结点,利用头插法将结点插入链表
87-
node = ListNode(carry % 10)
88-
node.next = dummy.next
86+
node = ListNode(carry % 10, dummy.next)
8987
dummy.next = node
9088
carry //= 10
9189
return dummy.next
@@ -101,7 +99,9 @@ class Solution:
10199
* public class ListNode {
102100
* int val;
103101
* ListNode next;
104-
* ListNode(int x) { val = x; }
102+
* ListNode() {}
103+
* ListNode(int val) { this.val = val; }
104+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
105105
* }
106106
*/
107107
class Solution {
@@ -115,12 +115,10 @@ class Solution {
115115
s2.push(l2.val);
116116
}
117117
int carry = 0;
118-
ListNode dummy = new ListNode(-1);
118+
ListNode dummy = new ListNode();
119119
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
120120
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
121-
// 创建结点,利用头插法将结点插入链表
122-
ListNode node = new ListNode(carry % 10);
123-
node.next = dummy.next;
121+
ListNode node = new ListNode(carry % 10, dummy.next);
124122
dummy.next = node;
125123
carry /= 10;
126124
}
@@ -129,6 +127,49 @@ class Solution {
129127
}
130128
```
131129

130+
### **C++**
131+
132+
```cpp
133+
/**
134+
* Definition for singly-linked list.
135+
* struct ListNode {
136+
* int val;
137+
* ListNode *next;
138+
* ListNode() : val(0), next(nullptr) {}
139+
* ListNode(int x) : val(x), next(nullptr) {}
140+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
146+
stack<int> s1;
147+
stack<int> s2;
148+
for (; l1; l1 = l1->next) s1.push(l1->val);
149+
for (; l2; l2 = l2->next) s2.push(l2->val);
150+
int carry = 0;
151+
ListNode* dummy = new ListNode();
152+
while (!s1.empty() || !s2.empty() || carry)
153+
{
154+
if (!s1.empty())
155+
{
156+
carry += s1.top();
157+
s1.pop();
158+
}
159+
if (!s2.empty())
160+
{
161+
carry += s2.top();
162+
s2.pop();
163+
}
164+
ListNode* node = new ListNode(carry % 10, dummy->next);
165+
dummy->next = node;
166+
carry /= 10;
167+
}
168+
return dummy->next;
169+
}
170+
};
171+
```
172+
132173
### **Go**
133174
134175
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14+
stack<int> s1;
15+
stack<int> s2;
16+
for (; l1; l1 = l1->next) s1.push(l1->val);
17+
for (; l2; l2 = l2->next) s2.push(l2->val);
18+
int carry = 0;
19+
ListNode* dummy = new ListNode();
20+
while (!s1.empty() || !s2.empty() || carry)
21+
{
22+
if (!s1.empty())
23+
{
24+
carry += s1.top();
25+
s1.pop();
26+
}
27+
if (!s2.empty())
28+
{
29+
carry += s2.top();
30+
s2.pop();
31+
}
32+
ListNode* node = new ListNode(carry % 10, dummy->next);
33+
dummy->next = node;
34+
carry /= 10;
35+
}
36+
return dummy->next;
37+
}
38+
};

lcof2/剑指 Offer II 025. 链表中的两数相加/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
3030
carry /= 10
3131
}
3232
return dummy.Next
33-
}
33+
}

lcof2/剑指 Offer II 025. 链表中的两数相加/Solution.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
@@ -17,11 +19,10 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
1719
s2.push(l2.val);
1820
}
1921
int carry = 0;
20-
ListNode dummy = new ListNode(-1);
22+
ListNode dummy = new ListNode();
2123
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
2224
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
23-
ListNode node = new ListNode(carry % 10);
24-
node.next = dummy.next;
25+
ListNode node = new ListNode(carry % 10, dummy.next);
2526
dummy.next = node;
2627
carry /= 10;
2728
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
87
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
98
s1, s2 = [], []
@@ -13,11 +12,10 @@ def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
1312
while l2:
1413
s2.append(l2.val)
1514
l2 = l2.next
16-
carry, dummy = 0, ListNode(-1)
15+
carry, dummy = 0, ListNode()
1716
while s1 or s2 or carry:
1817
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
19-
node = ListNode(carry % 10)
20-
node.next = dummy.next
18+
node = ListNode(carry % 10, dummy.next)
2119
dummy.next = node
2220
carry //= 10
2321
return dummy.next

solution/0400-0499/0445.Add Two Numbers II/README.md

+53-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<strong>输出:</strong>7 -&gt; 8 -&gt; 0 -&gt; 7
2525
</pre>
2626

27-
2827
## 解法
2928

3029
<!-- 这里可写通用的实现逻辑 -->
@@ -40,10 +39,9 @@
4039
```python
4140
# Definition for singly-linked list.
4241
# class ListNode:
43-
# def __init__(self, x):
44-
# self.val = x
45-
# self.next = None
46-
42+
# def __init__(self, val=0, next=None):
43+
# self.val = val
44+
# self.next = next
4745
class Solution:
4846
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
4947
s1, s2 = [], []
@@ -53,12 +51,11 @@ class Solution:
5351
while l2:
5452
s2.append(l2.val)
5553
l2 = l2.next
56-
carry, dummy = 0, ListNode(-1)
54+
carry, dummy = 0, ListNode()
5755
while s1 or s2 or carry:
5856
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
5957
# 创建结点,利用头插法将结点插入链表
60-
node = ListNode(carry % 10)
61-
node.next = dummy.next
58+
node = ListNode(carry % 10, dummy.next)
6259
dummy.next = node
6360
carry //= 10
6461
return dummy.next
@@ -74,7 +71,9 @@ class Solution:
7471
* public class ListNode {
7572
* int val;
7673
* ListNode next;
77-
* ListNode(int x) { val = x; }
74+
* ListNode() {}
75+
* ListNode(int val) { this.val = val; }
76+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7877
* }
7978
*/
8079
class Solution {
@@ -88,12 +87,10 @@ class Solution {
8887
s2.push(l2.val);
8988
}
9089
int carry = 0;
91-
ListNode dummy = new ListNode(-1);
90+
ListNode dummy = new ListNode();
9291
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
9392
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
94-
// 创建结点,利用头插法将结点插入链表
95-
ListNode node = new ListNode(carry % 10);
96-
node.next = dummy.next;
93+
ListNode node = new ListNode(carry % 10, dummy.next);
9794
dummy.next = node;
9895
carry /= 10;
9996
}
@@ -102,6 +99,49 @@ class Solution {
10299
}
103100
```
104101

102+
### **C++**
103+
104+
```cpp
105+
/**
106+
* Definition for singly-linked list.
107+
* struct ListNode {
108+
* int val;
109+
* ListNode *next;
110+
* ListNode() : val(0), next(nullptr) {}
111+
* ListNode(int x) : val(x), next(nullptr) {}
112+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
113+
* };
114+
*/
115+
class Solution {
116+
public:
117+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
118+
stack<int> s1;
119+
stack<int> s2;
120+
for (; l1; l1 = l1->next) s1.push(l1->val);
121+
for (; l2; l2 = l2->next) s2.push(l2->val);
122+
int carry = 0;
123+
ListNode* dummy = new ListNode();
124+
while (!s1.empty() || !s2.empty() || carry)
125+
{
126+
if (!s1.empty())
127+
{
128+
carry += s1.top();
129+
s1.pop();
130+
}
131+
if (!s2.empty())
132+
{
133+
carry += s2.top();
134+
s2.pop();
135+
}
136+
ListNode* node = new ListNode(carry % 10, dummy->next);
137+
dummy->next = node;
138+
carry /= 10;
139+
}
140+
return dummy->next;
141+
}
142+
};
143+
```
144+
105145
### **Go**
106146
107147
```go

0 commit comments

Comments
 (0)