Skip to content

Commit 9bb0a91

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典题解》:面试题 02.05. 链表求和
1 parent 027c931 commit 9bb0a91

File tree

4 files changed

+199
-46
lines changed

4 files changed

+199
-46
lines changed

lcci/02.05.Sum Lists/README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,64 @@
2727

2828
## 解法
2929
<!-- 这里可写通用的实现逻辑 -->
30-
30+
同时遍历两链表,求节点的和与进位。
3131

3232
### Python3
3333
<!-- 这里可写当前语言的特殊实现逻辑 -->
3434

3535
```python
36+
# Definition for singly-linked list.
37+
# class ListNode:
38+
# def __init__(self, x):
39+
# self.val = x
40+
# self.next = None
41+
42+
class Solution:
43+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
44+
p = ListNode(-1)
45+
carry, t = 0, p
46+
while l1 or l2:
47+
s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry
48+
carry = 1 if s > 9 else 0
49+
t.next = ListNode(s % 10)
50+
t = t.next
51+
l1 = l1.next if l1 else l1
52+
l2 = l2.next if l2 else l2
53+
t.next = None if carry == 0 else ListNode(carry)
54+
return p.next
55+
3656

3757
```
3858

3959
### Java
4060
<!-- 这里可写当前语言的特殊实现逻辑 -->
4161

4262
```java
43-
63+
/**
64+
* Definition for singly-linked list.
65+
* public class ListNode {
66+
* int val;
67+
* ListNode next;
68+
* ListNode(int x) { val = x; }
69+
* }
70+
*/
71+
class Solution {
72+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
73+
ListNode p = new ListNode(-1);
74+
int carry = 0;
75+
ListNode t = p;
76+
while (l1 != null || l2 != null) {
77+
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
78+
t.next = new ListNode(s % 10);
79+
carry = s > 9 ? 1 : 0;
80+
t = t.next;
81+
l1 = l1 == null ? l1 : l1.next;
82+
l2 = l2 == null ? l2 : l2.next;
83+
}
84+
t.next = carry == 0 ? null : new ListNode(carry);
85+
return p.next;
86+
}
87+
}
4488
```
4589

4690
### ...

lcci/02.05.Sum Lists/README_EN.md

+107-44
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,107 @@
1-
# [02.05. Sum Lists](https://leetcode-cn.com/problems/sum-lists-lcci)
2-
3-
## Description
4-
<p>You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1&#39;s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example: </strong></p>
9-
10-
<pre>
11-
<strong>Input: </strong>(7 -&gt; 1 -&gt; 6) + (5 -&gt; 9 -&gt; 2). That is, 617 + 295.
12-
<strong>Output: </strong>2 -&gt; 1 -&gt; 9. That is, 912.
13-
</pre>
14-
15-
<p><strong>Follow Up:&nbsp;</strong>Suppose the digits are stored in forward order. Repeat the above problem.</p>
16-
17-
<p><strong>Example: </strong></p>
18-
19-
<pre>
20-
<strong>Input: </strong>(6 -&gt; 1 -&gt; 7) + (2 -&gt; 9 -&gt; 5). That is, 617 + 295.
21-
<strong>Output: </strong>9 -&gt; 1 -&gt; 2. That is, 912.
22-
</pre>
23-
24-
25-
26-
## Solutions
27-
28-
29-
### Python3
30-
31-
```python
32-
33-
```
34-
35-
### Java
36-
37-
```java
38-
39-
```
40-
41-
### ...
42-
```
43-
44-
```
1+
# [02.05. Sum Lists](https://leetcode-cn.com/problems/sum-lists-lcci)
2+
3+
## Description
4+
<p>You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1&#39;s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example: </strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input: </strong>(7 -&gt; 1 -&gt; 6) + (5 -&gt; 9 -&gt; 2). That is, 617 + 295.
19+
20+
<strong>Output: </strong>2 -&gt; 1 -&gt; 9. That is, 912.
21+
22+
</pre>
23+
24+
25+
26+
<p><strong>Follow Up:&nbsp;</strong>Suppose the digits are stored in forward order. Repeat the above problem.</p>
27+
28+
29+
30+
<p><strong>Example: </strong></p>
31+
32+
33+
34+
<pre>
35+
36+
<strong>Input: </strong>(6 -&gt; 1 -&gt; 7) + (2 -&gt; 9 -&gt; 5). That is, 617 + 295.
37+
38+
<strong>Output: </strong>9 -&gt; 1 -&gt; 2. That is, 912.
39+
40+
</pre>
41+
42+
43+
44+
45+
## Solutions
46+
47+
48+
### Python3
49+
50+
```python
51+
# Definition for singly-linked list.
52+
# class ListNode:
53+
# def __init__(self, x):
54+
# self.val = x
55+
# self.next = None
56+
57+
class Solution:
58+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
59+
p = ListNode(-1)
60+
carry, t = 0, p
61+
while l1 or l2:
62+
s = (0 if l1 is None else l1.val) + (0 if l2 is None else l2.val) + carry
63+
carry = 1 if s > 9 else 0
64+
t.next = ListNode(s % 10)
65+
t = t.next
66+
l1 = l1.next if l1 else l1
67+
l2 = l2.next if l2 else l2
68+
t.next = None if carry == 0 else ListNode(carry)
69+
return p.next
70+
71+
72+
```
73+
74+
### Java
75+
76+
```java
77+
/**
78+
* Definition for singly-linked list.
79+
* public class ListNode {
80+
* int val;
81+
* ListNode next;
82+
* ListNode(int x) { val = x; }
83+
* }
84+
*/
85+
class Solution {
86+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
87+
ListNode p = new ListNode(-1);
88+
int carry = 0;
89+
ListNode t = p;
90+
while (l1 != null || l2 != null) {
91+
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
92+
t.next = new ListNode(s % 10);
93+
carry = s > 9 ? 1 : 0;
94+
t = t.next;
95+
l1 = l1 == null ? l1 : l1.next;
96+
l2 = l2 == null ? l2 : l2.next;
97+
}
98+
t.next = carry == 0 ? null : new ListNode(carry);
99+
return p.next;
100+
}
101+
}
102+
```
103+
104+
### ...
105+
```
106+
107+
```

lcci/02.05.Sum Lists/Solution.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11+
ListNode p = new ListNode(-1);
12+
int carry = 0;
13+
ListNode t = p;
14+
while (l1 != null || l2 != null) {
15+
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;
21+
}
22+
t.next = carry == 0 ? null : new ListNode(carry);
23+
return p.next;
24+
}
25+
}

lcci/02.05.Sum Lists/Solution.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
8+
class Solution:
9+
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

0 commit comments

Comments
 (0)