48
48
49
49
class Solution :
50
50
def addTwoNumbers (self , l1 : ListNode, l2 : ListNode) -> ListNode:
51
+ dummy = cur = ListNode(0 )
51
52
carry = 0
52
- dummy = ListNode(- 1 )
53
- cur = dummy
54
53
while l1 or l2 or carry:
55
- s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
56
- carry, val = divmod (s, 10 )
57
- cur.next = ListNode(val)
54
+ carry += (0 if not l1 else l1.val) + (0 if not l2 else l2.val)
55
+ cur.next = ListNode(carry % 10 )
58
56
cur = cur.next
57
+ carry //= 10
59
58
l1 = None if not l1 else l1.next
60
59
l2 = None if not l2 else l2.next
61
60
return dummy.next
@@ -80,7 +79,8 @@ class Solution {
80
79
ListNode dummy = new ListNode (- 1 );
81
80
ListNode cur = dummy;
82
81
while (l1 != null || l2 != null || carry != 0 ) {
83
- int s = (l1 == null ? 0 : l1. val) + (l2 == null ? 0 : l2. val) + carry;
82
+ int s =
83
+ (l1 == null ? 0 : l1. val) + (l2 == null ? 0 : l2. val) + carry;
84
84
carry = s / 10 ;
85
85
cur. next = new ListNode (s % 10 );
86
86
cur = cur. next;
@@ -106,16 +106,17 @@ class Solution {
106
106
class Solution {
107
107
public:
108
108
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
109
- int carry = 0;
110
- ListNode* dummy = new ListNode(-1);
109
+ ListNode* dummy = new ListNode(0);
111
110
ListNode* cur = dummy;
112
- while (l1 != NULL || l2 != NULL || carry != 0) {
113
- int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
114
- carry = s / 10;
115
- cur->next = new ListNode(s % 10);
111
+ int carry = 0;
112
+ while (l1 || l2 || carry)
113
+ {
114
+ carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val);
115
+ cur->next = new ListNode(carry % 10);
116
116
cur = cur->next;
117
- l1 = l1 == NULL ? NULL : l1->next;
118
- l2 = l2 == NULL ? NULL : l2->next;
117
+ carry /= 10;
118
+ l1 = l1 ? l1->next : l1;
119
+ l2 = l2 ? l2->next : l2;
119
120
}
120
121
return dummy->next;
121
122
}
@@ -139,20 +140,51 @@ public:
139
140
*/
140
141
var addTwoNumbers = function (l1, l2) {
141
142
let carry = 0;
142
- const dummy = new ListNode(-1 );
143
+ const dummy = new ListNode(0 );
143
144
let cur = dummy;
144
145
while (l1 || l2 || carry) {
145
- const s = (l1 ? l1 .val : 0) + (l2 ? l2 .val : 0) + carry ;
146
- carry = Math.floor(s / 10);
147
- cur.next = new ListNode(s % 10);
146
+ carry + = (l1? .val || 0) + (l2? .val || 0);
147
+ cur.next = new ListNode(carry % 10);
148
+ carry = Math.floor(carry / 10);
148
149
cur = cur.next;
149
- l1 = l1 ? l1 .next : l1 ;
150
- l2 = l2 ? l2 .next : l2 ;
150
+ l1 = l1? .next;
151
+ l2 = l2? .next;
151
152
}
152
153
return dummy.next;
153
154
};
154
155
```
155
156
157
+ ### ** Go**
158
+
159
+ ``` go
160
+ /* *
161
+ * Definition for singly-linked list.
162
+ * type ListNode struct {
163
+ * Val int
164
+ * Next *ListNode
165
+ * }
166
+ */
167
+ func addTwoNumbers (l1 *ListNode , l2 *ListNode ) *ListNode {
168
+ dummy := &ListNode{}
169
+ cur := dummy
170
+ carry := 0
171
+ for l1 != nil || l2 != nil || carry > 0 {
172
+ if l1 != nil {
173
+ carry += l1.Val
174
+ l1 = l1.Next
175
+ }
176
+ if l2 != nil {
177
+ carry += l2.Val
178
+ l2 = l2.Next
179
+ }
180
+ cur.Next = &ListNode{Val: carry % 10 }
181
+ cur = cur.Next
182
+ carry /= 10
183
+ }
184
+ return dummy.Next
185
+ }
186
+ ```
187
+
156
188
### ** ...**
157
189
158
190
```
0 commit comments