问题描述
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
1 | Example: |
给出两个非空的非负的整数链表。他们所代表了数字不同位数的反序存储,并且每个节点只能存储一位数字。
现在你要将这两个数字想加,返回一个新的链表表示它们的和。
你可以假设除了数字0之外,这两个数都不会以0开头。
解法
1 | // typescript |
运行情况:
1 | Runtime: 124 ms, faster than 81.49% of JavaScript online submissions for Add Two Numbers. |
复杂度分析:
时间内负责度: O(max(m, n)). 假设 m 和 n 分别表示 l1 和 l2 的长度,上面的算法最多从重复了 max(m, n) 次。
空间复杂度: O(max(m, n)). 新列表的长度最大为 max(m, n) + 1.
总结:
这道题比较简单,没什么好说的….
不过反向思考,这道题是否可以解决大数想加问题,比如 2^63 * 2^63 之类的运算 ? 举一反三,做完题多思考