Skip to content

Commit 02aee9a

Browse files
committed
feat: update lcci/lc solutions: 0205. Sum Lists
1 parent 407269a commit 02aee9a

File tree

12 files changed

+420
-168
lines changed

12 files changed

+420
-168
lines changed

lcci/02.05.Sum Lists/README.md

+81-23
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@
4848

4949
class Solution:
5050
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
51-
p = ListNode(-1)
52-
carry, t = 0, p
53-
while l1 or l2:
54-
s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry
55-
carry = 1 if s > 9 else 0
56-
t.next = ListNode(s % 10)
57-
t = t.next
58-
l1 = l1.next if l1 else l1
59-
l2 = l2.next if l2 else l2
60-
t.next = None if carry == 0 else ListNode(carry)
61-
return p.next
62-
63-
51+
carry = 0
52+
dummy = ListNode(-1)
53+
cur = dummy
54+
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)
58+
cur = cur.next
59+
l1 = None if not l1 else l1.next
60+
l2 = None if not l2 else l2.next
61+
return dummy.next
6462
```
6563

6664
### **Java**
@@ -78,23 +76,83 @@ class Solution:
7876
*/
7977
class Solution {
8078
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
81-
ListNode p = new ListNode(-1);
8279
int carry = 0;
83-
ListNode t = p;
84-
while (l1 != null || l2 != null) {
80+
ListNode dummy = new ListNode(-1);
81+
ListNode cur = dummy;
82+
while (l1 != null || l2 != null || carry != 0) {
8583
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
86-
t.next = new ListNode(s % 10);
87-
carry = s > 9 ? 1 : 0;
88-
t = t.next;
89-
l1 = l1 == null ? l1 : l1.next;
90-
l2 = l2 == null ? l2 : l2.next;
84+
carry = s / 10;
85+
cur.next = new ListNode(s % 10);
86+
cur = cur.next;
87+
l1 = l1 == null ? null : l1.next;
88+
l2 = l2 == null ? null : l2.next;
9189
}
92-
t.next = carry == 0 ? null : new ListNode(carry);
93-
return p.next;
90+
return dummy.next;
9491
}
9592
}
9693
```
9794

95+
### **C++**
96+
97+
```cpp
98+
/**
99+
* Definition for singly-linked list.
100+
* struct ListNode {
101+
* int val;
102+
* ListNode *next;
103+
* ListNode(int x) : val(x), next(NULL) {}
104+
* };
105+
*/
106+
class Solution {
107+
public:
108+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
109+
int carry = 0;
110+
ListNode* dummy = new ListNode(-1);
111+
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);
116+
cur = cur->next;
117+
l1 = l1 == NULL ? NULL : l1->next;
118+
l2 = l2 == NULL ? NULL : l2->next;
119+
}
120+
return dummy->next;
121+
}
122+
};
123+
```
124+
125+
### **JavaScript**
126+
127+
```js
128+
/**
129+
* Definition for singly-linked list.
130+
* function ListNode(val) {
131+
* this.val = val;
132+
* this.next = null;
133+
* }
134+
*/
135+
/**
136+
* @param {ListNode} l1
137+
* @param {ListNode} l2
138+
* @return {ListNode}
139+
*/
140+
var addTwoNumbers = function (l1, l2) {
141+
let carry = 0;
142+
const dummy = new ListNode(-1);
143+
let cur = dummy;
144+
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);
148+
cur = cur.next;
149+
l1 = l1 ? l1.next : l1;
150+
l2 = l2 ? l2.next : l2;
151+
}
152+
return dummy.next;
153+
};
154+
```
155+
98156
### **...**
99157

100158
```

lcci/02.05.Sum Lists/README_EN.md

+81-23
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,17 @@
4545

4646
class Solution:
4747
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
48-
p = ListNode(-1)
49-
carry, t = 0, p
50-
while l1 or l2:
51-
s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry
52-
carry = 1 if s > 9 else 0
53-
t.next = ListNode(s % 10)
54-
t = t.next
55-
l1 = l1.next if l1 else l1
56-
l2 = l2.next if l2 else l2
57-
t.next = None if carry == 0 else ListNode(carry)
58-
return p.next
59-
60-
48+
carry = 0
49+
dummy = ListNode(-1)
50+
cur = dummy
51+
while l1 or l2 or carry:
52+
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
53+
carry, val = divmod(s, 10)
54+
cur.next = ListNode(val)
55+
cur = cur.next
56+
l1 = None if not l1 else l1.next
57+
l2 = None if not l2 else l2.next
58+
return dummy.next
6159
```
6260

6361
### **Java**
@@ -73,23 +71,83 @@ class Solution:
7371
*/
7472
class Solution {
7573
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
76-
ListNode p = new ListNode(-1);
7774
int carry = 0;
78-
ListNode t = p;
79-
while (l1 != null || l2 != null) {
75+
ListNode dummy = new ListNode(-1);
76+
ListNode cur = dummy;
77+
while (l1 != null || l2 != null || carry != 0) {
8078
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
81-
t.next = new ListNode(s % 10);
82-
carry = s > 9 ? 1 : 0;
83-
t = t.next;
84-
l1 = l1 == null ? l1 : l1.next;
85-
l2 = l2 == null ? l2 : l2.next;
79+
carry = s / 10;
80+
cur.next = new ListNode(s % 10);
81+
cur = cur.next;
82+
l1 = l1 == null ? null : l1.next;
83+
l2 = l2 == null ? null : l2.next;
8684
}
87-
t.next = carry == 0 ? null : new ListNode(carry);
88-
return p.next;
85+
return dummy.next;
8986
}
9087
}
9188
```
9289

90+
### **C++**
91+
92+
```cpp
93+
/**
94+
* Definition for singly-linked list.
95+
* struct ListNode {
96+
* int val;
97+
* ListNode *next;
98+
* ListNode(int x) : val(x), next(NULL) {}
99+
* };
100+
*/
101+
class Solution {
102+
public:
103+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
104+
int carry = 0;
105+
ListNode* dummy = new ListNode(-1);
106+
ListNode* cur = dummy;
107+
while (l1 != NULL || l2 != NULL || carry != 0) {
108+
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
109+
carry = s / 10;
110+
cur->next = new ListNode(s % 10);
111+
cur = cur->next;
112+
l1 = l1 == NULL ? NULL : l1->next;
113+
l2 = l2 == NULL ? NULL : l2->next;
114+
}
115+
return dummy->next;
116+
}
117+
};
118+
```
119+
120+
### **JavaScript**
121+
122+
```js
123+
/**
124+
* Definition for singly-linked list.
125+
* function ListNode(val) {
126+
* this.val = val;
127+
* this.next = null;
128+
* }
129+
*/
130+
/**
131+
* @param {ListNode} l1
132+
* @param {ListNode} l2
133+
* @return {ListNode}
134+
*/
135+
var addTwoNumbers = function (l1, l2) {
136+
let carry = 0;
137+
const dummy = new ListNode(-1);
138+
let cur = dummy;
139+
while (l1 || l2 || carry) {
140+
const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
141+
carry = Math.floor(s / 10);
142+
cur.next = new ListNode(s % 10);
143+
cur = cur.next;
144+
l1 = l1 ? l1.next : l1;
145+
l2 = l2 ? l2.next : l2;
146+
}
147+
return dummy.next;
148+
};
149+
```
150+
93151
### **...**
94152

95153
```

lcci/02.05.Sum Lists/Solution.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12+
int carry = 0;
13+
ListNode* dummy = new ListNode(-1);
14+
ListNode* cur = dummy;
15+
while (l1 != NULL || l2 != NULL || carry != 0) {
16+
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
17+
carry = s / 10;
18+
cur->next = new ListNode(s % 10);
19+
cur = cur->next;
20+
l1 = l1 == NULL ? NULL : l1->next;
21+
l2 = l2 == NULL ? NULL : l2->next;
22+
}
23+
return dummy->next;
24+
}
25+
};

lcci/02.05.Sum Lists/Solution.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
*/
99
class Solution {
1010
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11-
ListNode p = new ListNode(-1);
1211
int carry = 0;
13-
ListNode t = p;
14-
while (l1 != null || l2 != null) {
12+
ListNode dummy = new ListNode(-1);
13+
ListNode cur = dummy;
14+
while (l1 != null || l2 != null || carry != 0) {
1515
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
16-
t.next = new ListNode(s % 10);
17-
carry = s > 9 ? 1 : 0;
18-
t = t.next;
19-
l1 = l1 == null ? l1 : l1.next;
20-
l2 = l2 == null ? l2 : l2.next;
16+
carry = s / 10;
17+
cur.next = new ListNode(s % 10);
18+
cur = cur.next;
19+
l1 = l1 == null ? null : l1.next;
20+
l2 = l2 == null ? null : l2.next;
2121
}
22-
t.next = carry == 0 ? null : new ListNode(carry);
23-
return p.next;
22+
return dummy.next;
2423
}
2524
}

lcci/02.05.Sum Lists/Solution.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function (l1, l2) {
14+
let carry = 0;
15+
const dummy = new ListNode(-1);
16+
let cur = dummy;
17+
while (l1 || l2 || carry) {
18+
const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
19+
carry = Math.floor(s / 10);
20+
cur.next = new ListNode(s % 10);
21+
cur = cur.next;
22+
l1 = l1 ? l1.next : l1;
23+
l2 = l2 ? l2.next : l2;
24+
}
25+
return dummy.next;
26+
};

lcci/02.05.Sum Lists/Solution.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
# self.val = x
55
# self.next = None
66

7-
87
class Solution:
98
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
10-
p = ListNode(-1)
11-
carry, t = 0, p
12-
while l1 or l2:
13-
s = (0 if l1 is None else l1.val) + \
14-
(0 if l2 is None else l2.val) + carry
15-
carry = 1 if s > 9 else 0
16-
t.next = ListNode(s % 10)
17-
t = t.next
18-
l1 = l1.next if l1 else l1
19-
l2 = l2.next if l2 else l2
20-
t.next = None if carry == 0 else ListNode(carry)
21-
return p.next
9+
carry = 0
10+
dummy = ListNode(-1)
11+
cur = dummy
12+
while l1 or l2 or carry:
13+
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
14+
carry, val = divmod(s, 10)
15+
cur.next = ListNode(val)
16+
cur = cur.next
17+
l1 = None if not l1 else l1.next
18+
l2 = None if not l2 else l2.next
19+
return dummy.next

0 commit comments

Comments
 (0)