24
24
<strong >输出:</strong >7 -> ; 8 -> ; 0 -> ; 7
25
25
</pre >
26
26
27
-
28
27
## 解法
29
28
30
29
<!-- 这里可写通用的实现逻辑 -->
40
39
``` python
41
40
# Definition for singly-linked list.
42
41
# 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
47
45
class Solution :
48
46
def addTwoNumbers (self , l1 : ListNode, l2 : ListNode) -> ListNode:
49
47
s1, s2 = [], []
@@ -53,12 +51,11 @@ class Solution:
53
51
while l2:
54
52
s2.append(l2.val)
55
53
l2 = l2.next
56
- carry, dummy = 0 , ListNode(- 1 )
54
+ carry, dummy = 0 , ListNode()
57
55
while s1 or s2 or carry:
58
56
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
59
57
# 创建结点,利用头插法将结点插入链表
60
- node = ListNode(carry % 10 )
61
- node.next = dummy.next
58
+ node = ListNode(carry % 10 , dummy.next)
62
59
dummy.next = node
63
60
carry //= 10
64
61
return dummy.next
@@ -74,7 +71,9 @@ class Solution:
74
71
* public class ListNode {
75
72
* int val;
76
73
* 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; }
78
77
* }
79
78
*/
80
79
class Solution {
@@ -88,12 +87,10 @@ class Solution {
88
87
s2. push(l2. val);
89
88
}
90
89
int carry = 0 ;
91
- ListNode dummy = new ListNode (- 1 );
90
+ ListNode dummy = new ListNode ();
92
91
while (! s1. isEmpty() || ! s2. isEmpty() || carry != 0 ) {
93
92
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);
97
94
dummy. next = node;
98
95
carry /= 10 ;
99
96
}
@@ -102,6 +99,49 @@ class Solution {
102
99
}
103
100
```
104
101
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
+
105
145
### **Go**
106
146
107
147
```go
0 commit comments