Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b8f16be

Browse files
Merge pull request SharingSource#465 from SharingSource/ac_oier
✨feat: Add 面试题 02.05
2 parents 591867b + 9db96f5 commit b8f16be

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

‎Index/栈.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
1010
| [1190. 反转每对括号间的子串](https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses/solution/gong-shui-san-xie-shi-yong-shuang-duan-d-r35q/) | 中等 | 🤩🤩🤩🤩🤩 |
1111
| [面试题 03.01. 三合一](https://leetcode-cn.com/problems/three-in-one-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/three-in-one-lcci/solution/yi-ti-shuang-jie-er-wei-shu-zu-yi-wei-sh-lih7/) | 简单 | 🤩🤩🤩 |
12+
| [面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/) | 中等 | 🤩🤩🤩 |
1213

‎Index/链表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
| [1600. 皇位继承顺序](https://leetcode-cn.com/problems/throne-inheritance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/throne-inheritance/solution/gong-shui-san-xie-shi-yong-dan-xiang-lia-7t65/) | 中等 | 🤩🤩🤩 |
2424
| [剑指 Offer 22. 链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solution/gong-shui-san-xie-yi-ti-san-jie-zhan-dui-w3rz/) | 简单 | 🤩🤩🤩🤩🤩 |
2525
| [剑指 Offer 52. 两个链表的第一个公共节点](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/solution/gong-shui-san-xie-zhao-liang-tiao-lian-b-ifqw/) | 简单 | 🤩🤩🤩🤩🤩 |
26+
| [面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/) | 中等 | 🤩🤩🤩🤩🤩 |
2627

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/)** ,难度为 **中等**
4+
5+
Tag : 「链表」、「模拟」
6+
7+
8+
9+
给定两个用链表表示的整数,每个节点包含一个数位。
10+
11+
这些数位是反向存放的,也就是个位排在链表首部。
12+
13+
编写函数对这两个整数求和,并用链表形式返回结果。
14+
15+
示例:
16+
```
17+
输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295
18+
19+
输出:2 -> 1 -> 9,即912
20+
```
21+
22+
示例:
23+
```
24+
输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295
25+
26+
输出:9 -> 1 -> 2,即912
27+
```
28+
29+
进阶:思考一下,假设这些数位是正向存放的,又该如何解决呢?
30+
31+
---
32+
33+
### 链表(反向)
34+
35+
访问链表节点的顺序为「个位、百位、千位 ...」,即与执行「竖式加法」时访问的位数次序一致。
36+
37+
因此我们可以边遍历,边模拟「竖式加法」的过程,使用变量 $t$ 存储进位情况,当 $l1$ 和 $l2$ 没遍历完,或者进位 $t$ 不为 0ドル,ドル该计算过程就可以继续,每次使用当前计算结果 `t % 10` 来创建新节点,使用 `t / 10` 来更新进位。
38+
39+
代码:
40+
```Java
41+
class Solution {
42+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
43+
ListNode ans = new ListNode(-1), temp = ans;
44+
int t = 0;
45+
while (l1 != null || l2 != null || t != 0) {
46+
if (l1 != null) {
47+
t += l1.val;
48+
l1 = l1.next;
49+
}
50+
if (l2 != null) {
51+
t += l2.val;
52+
l2 = l2.next;
53+
}
54+
temp.next = new ListNode(t % 10);
55+
temp = temp.next;
56+
t /= 10;
57+
}
58+
return ans.next;
59+
}
60+
}
61+
```
62+
* 时间复杂度:令 $n$ 为链表 `l1` 的长度,$m$ 为链表 `l2` 的长度,复杂度为 $O(\max(n, m))$
63+
* 空间复杂度:$O(1)$
64+
65+
---
66+
67+
### 链表(正向)
68+
69+
如果将链表翻转(先访问到的是数值高位),该如何处理?
70+
71+
由于我们的「竖式加法」是从低位开始,因此我们需要先使用数据结构(栈 / 数组)对链表元素进行存储,再实现「从低位访问」并进行「竖式加法」的模拟过程。
72+
73+
为了验证代码正确性,我们可以先对 `l1``l2` 进行翻转,再走链表(正向)的处理逻辑。
74+
75+
代码:
76+
```Java
77+
class Solution {
78+
ListNode reverse(ListNode node) {
79+
ListNode prev = null, cur = node;
80+
while (cur != null) {
81+
ListNode temp = cur.next;
82+
cur.next = prev;
83+
prev = cur; cur = temp;
84+
}
85+
return prev;
86+
}
87+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
88+
l1 = reverse(l1);
89+
l2 = reverse(l2);
90+
91+
Deque<Integer> d1 = new ArrayDeque<>(), d2 = new ArrayDeque<>();
92+
while (l1 != null) {
93+
d1.addLast(l1.val);
94+
l1 = l1.next;
95+
}
96+
while (l2 != null) {
97+
d2.addLast(l2.val);
98+
l2 = l2.next;
99+
}
100+
ListNode ans = new ListNode(-1), temp = ans;
101+
int t = 0;
102+
while (!d1.isEmpty() || !d2.isEmpty() || t != 0) {
103+
if (!d1.isEmpty()) t += d1.pollLast();
104+
if (!d2.isEmpty()) t += d2.pollLast();
105+
temp.next = new ListNode(t % 10);
106+
temp = temp.next;
107+
t /= 10;
108+
}
109+
return ans.next;
110+
}
111+
}
112+
```
113+
* 时间复杂度:令 $n$ 为链表 `l1` 的长度,$m$ 为链表 `l2` 的长度,复杂度为 $O(\max(n, m))$
114+
* 空间复杂度:$O(n + m)$
115+
116+
---
117+
118+
### 最后
119+
120+
这是我们「刷穿 LeetCode」系列文章的第 `面试题 02.05. 链表求和` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
121+
122+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
123+
124+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
125+
126+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
127+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /