From 9db96f540e9f840c853405ab39897cd0cb01adce Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Wed, 13 Apr 2022 10:25:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20=E9=9D=A2=E8=AF=95=E9=A2=98=20?= =?UTF-8?q?02.05?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\240\210.md" | 1 + "Index/\351\223\276\350\241\250.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 127 ++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 "LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 02.05. \351\223\276\350\241\250\346\261\202\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\346\240\210.md" "b/Index/\346\240\210.md" index 520fb023..67e7bcbd 100644 --- "a/Index/\346\240\210.md" +++ "b/Index/\346\240\210.md" @@ -9,4 +9,5 @@ | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [面试题 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/) | 简单 | 🤩🤩🤩 | +| [面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/) | 中等 | 🤩🤩🤩 | diff --git "a/Index/\351\223\276\350\241\250.md" "b/Index/\351\223\276\350\241\250.md" index 22c8a131..dddd63f2 100644 --- "a/Index/\351\223\276\350\241\250.md" +++ "b/Index/\351\223\276\350\241\250.md" @@ -23,4 +23,5 @@ | [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/) | 中等 | 🤩🤩🤩 | | [剑指 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/) | 简单 | 🤩🤩🤩🤩🤩 | | [剑指 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/) | 简单 | 🤩🤩🤩🤩🤩 | +| [面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 02.05. \351\223\276\350\241\250\346\261\202\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 02.05. \351\223\276\350\241\250\346\261\202\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..a10ed272 --- /dev/null +++ "b/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 02.05. \351\223\276\350\241\250\346\261\202\345\222\214\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,127 @@ +### 题目描述 + +这是 LeetCode 上的 **[面试题 02.05. 链表求和](https://leetcode-cn.com/problems/sum-lists-lcci/solution/by-ac_oier-v1zb/)** ,难度为 **中等**。 + +Tag : 「链表」、「模拟」 + + + +给定两个用链表表示的整数,每个节点包含一个数位。 + +这些数位是反向存放的,也就是个位排在链表首部。 + +编写函数对这两个整数求和,并用链表形式返回结果。 + +示例: +``` +输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295 + +输出:2 -> 1 -> 9,即912 +``` + +示例: +``` +输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295 + +输出:9 -> 1 -> 2,即912 +``` + +进阶:思考一下,假设这些数位是正向存放的,又该如何解决呢? + +--- + +### 链表(反向) + +访问链表节点的顺序为「个位、百位、千位 ...」,即与执行「竖式加法」时访问的位数次序一致。 + +因此我们可以边遍历,边模拟「竖式加法」的过程,使用变量 $t$ 存储进位情况,当 $l1$ 和 $l2$ 没遍历完,或者进位 $t$ 不为 $0$,该计算过程就可以继续,每次使用当前计算结果 `t % 10` 来创建新节点,使用 `t / 10` 来更新进位。 + +代码: +```Java +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode ans = new ListNode(-1), temp = ans; + int t = 0; + while (l1 != null || l2 != null || t != 0) { + if (l1 != null) { + t += l1.val; + l1 = l1.next; + } + if (l2 != null) { + t += l2.val; + l2 = l2.next; + } + temp.next = new ListNode(t % 10); + temp = temp.next; + t /= 10; + } + return ans.next; + } +} +``` +* 时间复杂度:令 $n$ 为链表 `l1` 的长度,$m$ 为链表 `l2` 的长度,复杂度为 $O(\max(n, m))$ +* 空间复杂度:$O(1)$ + +--- + +### 链表(正向) + +如果将链表翻转(先访问到的是数值高位),该如何处理? + +由于我们的「竖式加法」是从低位开始,因此我们需要先使用数据结构(栈 / 数组)对链表元素进行存储,再实现「从低位访问」并进行「竖式加法」的模拟过程。 + +为了验证代码正确性,我们可以先对 `l1` 和 `l2` 进行翻转,再走链表(正向)的处理逻辑。 + +代码: +```Java +class Solution { + ListNode reverse(ListNode node) { + ListNode prev = null, cur = node; + while (cur != null) { + ListNode temp = cur.next; + cur.next = prev; + prev = cur; cur = temp; + } + return prev; + } + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + l1 = reverse(l1); + l2 = reverse(l2); + + Deque d1 = new ArrayDeque<>(), d2 = new ArrayDeque<>(); + while (l1 != null) { + d1.addLast(l1.val); + l1 = l1.next; + } + while (l2 != null) { + d2.addLast(l2.val); + l2 = l2.next; + } + ListNode ans = new ListNode(-1), temp = ans; + int t = 0; + while (!d1.isEmpty() || !d2.isEmpty() || t != 0) { + if (!d1.isEmpty()) t += d1.pollLast(); + if (!d2.isEmpty()) t += d2.pollLast(); + temp.next = new ListNode(t % 10); + temp = temp.next; + t /= 10; + } + return ans.next; + } +} +``` +* 时间复杂度:令 $n$ 为链表 `l1` 的长度,$m$ 为链表 `l2` 的长度,复杂度为 $O(\max(n, m))$ +* 空间复杂度:$O(n + m)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `面试题 02.05. 链表求和` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +