445.add-two-numbers-ii.md 3.0 KB
Newer Older
L
luzhipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

## 题目地址
https://leetcode.com/problems/add-two-numbers-ii/description/

## 题目描述

```
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

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

```
## 思路

由于需要从低位开始加,然后进位。 因此可以采用栈来简化操作。
依次将两个链表的值分别入栈stack1和stack2,然后相加入栈stack,进位操作用一个变量carried记录即可。

最后根据stack生成最终的链表即可。

## 关键点解析

- 栈的基本操作
- carried 变量记录进位
- 循环的终止条件设置成`stack.length > 0` 可以简化操作
- 注意特殊情况, 比如 1 + 99 = 100

## 代码
```js
/*
 * @lc app=leetcode id=445 lang=javascript
 *
 * [445] Add Two Numbers II
 *
 * https://leetcode.com/problems/add-two-numbers-ii/description/
 *
 * algorithms
 * Medium (49.31%)
 * Total Accepted:    83.7K
 * Total Submissions: 169K
 * Testcase Example:  '[7,2,4,3]\n[5,6,4]'
 *
 * You are given two non-empty linked lists representing two non-negative
 * integers. The most significant digit comes first 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.
 * 
 * Follow up:
 * What if you cannot modify the input lists? In other words, reversing the
 * lists is not allowed.
 * 
 * 
 * 
 * Example:
 * 
 * Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
 * Output: 7 -> 8 -> 0 -> 7
 * 
 * 
 */
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    const stack1 = [];
    const stack2 = [];
    const stack = [];

    let cur1 = l1;
    let cur2 = l2;
    let curried = 0;

    while(cur1) {
        stack1.push(cur1.val);
        cur1 = cur1.next;
    }

    while(cur2) {
        stack2.push(cur2.val);
        cur2 = cur2.next;
    }

    let a = null;
    let b = null;

    while(stack1.length > 0 || stack2.length > 0) {
        a = Number(stack1.pop()) || 0;
        b = Number(stack2.pop()) || 0;

        stack.push((a + b + curried) % 10);

        if (a + b + curried >= 10) {
            curried = 1;
        } else {
            curried = 0;
        }
    }

    if (curried === 1) {
        stack.push(1);
    }

    const dummy = {};

    let current = dummy;

    while(stack.length > 0) {
        current.next = {
            val: stack.pop(),
            next: null
        }

        current = current.next
    }

    return dummy.next;
};

```