[LeetCode] 445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]

Example 2:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]

Example 3:

Input: l1 = [0], l2 = [0]
Output: [0]

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

Follow up: Could you solve it without reversing the input lists?

两数相加 II。

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/add-two-numbers-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这题思路跟[LeetCode] 2. Add Two Numbers很相似。唯一的不同点在于,第2题是从最低位到最高位做加法,只要记得进位即可;但是445是在逆向做加法,linked list的头结点是数字的最高位,先reverse两个list然后再做加法的思路会略显麻烦。如下这个例子实际是在算 7243 + 564 = 7807

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

这题的思路是先用两个 stack 分别存住两个 list 的 nodes,然后 pop 出来的时候做加法。这样就不需要操心 reverse linked list 这件事了。但是这个题依然很麻烦,(照着这个例子讲)算出了个位数7之后,算十位数的时候,要把十位数的next指针再指向个位数,cur指针再从个位数移动到十位数。实际上cur指针是从右往左推进的(7 <- 8 <- 0 <- 7)。具体参见代码注释。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 class Solution {
12     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13         // 7, 2, 4, 3
14         // 5, 6, 4
15         Stack<Integer> s1 = new Stack<>();
16         Stack<Integer> s2 = new Stack<>();
17         while (l1 != null) {
18             s1.push(l1.val);
19             l1 = l1.next;
20         }
21         while (l2 != null) {
22             s2.push(l2.val);
23             l2 = l2.next;
24         }
25         // s1, 3, 4, 2, 7,    3 is on top
26         // s2, 4, 6, 5        4 is on top
27         
28         int sum = 0;
29         ListNode cur = new ListNode(0);
30         while (!s1.isEmpty() || !s2.isEmpty()) {
31             // 比如第一次计算,就是3+4=7
32             if (!s1.isEmpty()) {
33                 sum += s1.pop();
34             }
35             if (!s2.isEmpty()) {
36                 sum += s2.pop();
37             }
38             // cur.val 计算结果的个位数
39             cur.val = sum % 10;
40             // head.val 计算结果的十位数
41             ListNode head = new ListNode(sum / 10);
42             // 将十位数和个位数的node连在一起
43             head.next = cur;
44             // 走到十位数那个位置上做下一步运算
45             cur = head;
46             sum /= 10;
47         }
48         // 最后如果十位数的位置是0,说明这一位是无效的,返回他的下一位的node
49         if (cur.val == 0) {
50             return cur.next;
51         }
52         return cur;
53     }
54 }

 

JavaScript实现

 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     // 7, 2, 4, 3
15     // 5, 6, 4
16     let s1 = [];
17     let s2 = [];
18     while (l1 != null) {
19         s1.push(l1.val);
20         l1 = l1.next;
21     }
22     while (l2 != null) {
23         s2.push(l2.val);
24         l2 = l2.next;
25     }
26     // s1, 3, 4, 2, 7,    3 is on top
27     // s2, 4, 6, 5        4 is on top
28 
29     let cur = new ListNode(0);
30     let sum = 0;
31     while (s1.length > 0 || s2.length > 0) {
32         // 比如第一次计算,就是3+4=7
33         if (s1.length > 0) {
34             sum += s1.pop();
35         }
36         if (s2.length > 0) {
37             sum += s2.pop();
38         }
39         // cur.val 计算结果的个位数
40         cur.val = sum % 10;
41         // head.val 计算结果的十位数
42         let head = new ListNode(parseInt(sum / 10));
43         // 将十位数和个位数的node连在一起
44         head.next = cur;
45         // 走到十位数那个位置上做下一步运算
46         cur = head;
47         sum = parseInt(sum / 10);
48     }
49     // 最后如果十位数的位置是0,说明这一位是无效的,返回他的下一位的node
50     if (cur.val == 0) {
51         return cur.next;
52     } else {
53         return cur;
54     }
55 };

 

相关题目

2. Add Two Numbers

445. Add Two Numbers II

21. Merge Two Sorted Lists

1634. Add Two Polynomials Represented as Linked Lists

LeetCode 题目总结

posted @ 2019-10-13 14:42  CNoodle  阅读(228)  评论(0编辑  收藏  举报