solution.md 5.9 KB
Newer Older
1
# 两数相加
F
fix bug  
feilong 已提交
2

3
<p>给你两个 <strong>非空</strong> 的链表,表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的,并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加,并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外,这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg" style="width: 483px; height: 342px;" /><pre><strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出:</strong>[7,0,8]<strong><br />解释:</strong>342 + 465 = 807.</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>l1 = [0], l2 = [0]<strong><br />输出:</strong>[0]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出:</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li>每个链表中的节点数在范围 <code>[1, 100]</code> 内</li>	<li><code>0 <= Node.val <= 9</code></li>	<li>题目数据保证列表表示的数字不含前导零</li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
4
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
```cpp
#include <unordered_map>
#include <vector>
#include <iostream>
#include <map>
using namespace std;

struct ListNode
{
    int val;
    struct ListNode *next;
    ListNode() : val(0), next(nullptr){};
    ListNode(int x) : val(x), next(nullptr){};
    ListNode(int x, ListNode *next) : val(x), next(next){};
};
```
### after
F
fix bug  
feilong 已提交
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
```cpp

int main()
{
    Solution test;
    ListNode *L1 = new ListNode();
    ListNode *l11 = new ListNode(2);
    ListNode *l12 = new ListNode(4);
    ListNode *l13 = new ListNode(3);

    L1->next = l11;
    l11->next = l12;
    l12->next = l13;

    ListNode *L2 = new ListNode();
    ListNode *l21 = new ListNode(5);
    ListNode *l22 = new ListNode(6);
    ListNode *l23 = new ListNode(4);

    L2->next = l21;
    l21->next = l22;
    l22->next = l23;

    ListNode *ret = new ListNode();

    ret = test.addTwoNumbers(L1, L2);

    ListNode *p = ret->next;

    while (p != NULL)
    {
        cout << p->val << endl;
        p = p->next;
    }

    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
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
```cpp
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode *l3 = new ListNode(0);
        ListNode *p1 = l1;
        ListNode *p2 = l2;
        ListNode *p3 = l3;

        int sum = 0, carry = 0;
        while (p1 != NULL || p2 != NULL)
        {
            int x = (p1 != NULL) ? p1->val : 0;
            int y = (p2 != NULL) ? p2->val : 0;
            sum = x + y + carry;
            carry = sum / 10;
            p3->next = new ListNode(sum % 10);
            p3 = p3->next;
            if (p1 != NULL)
            {
                p1 = p1->next;
            }
            if (p2 != NULL)
            {
                p2 = p2->next;
            }
        }
        if (carry == 0)
        {
            p3->next = new ListNode(carry);
            p3 = p3->next;
        }
        return l3->next;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
109

110
### A
F
fix bug  
feilong 已提交
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
```cpp
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode *retList = new ListNode(0);
        auto tempList = retList;
        int newVal;
        int val1;
        int val2;
        int carryBit = 0;
        while (l1 != nullptr || l2 != nullptr)
        {
            if (l1 == nullptr)
                val1 = 0;
            else
            {
                val1 = l1->val;
                l1 = l1->next;
            }
            if (l2 == nullptr)
                val2 = 0;
            else
            {
                val2 = l2->val;
                l2 = l2->next;
            }
            newVal = (val1 + val2 + carryBit) % 10;
            carryBit = (val1 + val2 + carryBit) / 10;
            tempList->next = new ListNode(newVal);
            tempList = tempList->next;
        }

        if (carryBit == 1)
        {
            tempList->next = new ListNode(1);
        }
        return retList->next;
    }
};
```

### B
F
fix bug  
feilong 已提交
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
```cpp
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode *l3 = new ListNode;
        ListNode *l4 = l3;
        int m = 0;
        while (l1 != nullptr || l2 != nullptr)
        {
            if (l1 != nullptr && l2 != nullptr)
            {
                l3->val = (l1->val + l2->val + m) % 10;
                m = (l1->val + l2->val + m) / 10;
                l1 = l1->next;
                l2 = l2->next;
            }
            else if (l1 != nullptr)
            {
                l3->val = (l1->val + m) % 10;
                m = (l1->val + m) / 10;
                l1 = l1->next;
            }
            else if (l2 != nullptr)
            {
                l3->val = (l2->val + m) % 10;
                m = (l2->val + m) / 10;

                l2 = l2->next;
            }

            if (l1 != nullptr || l2 != nullptr)
            {
                l3->next = new ListNode;
                l3 = l3->next;
            }
            else
            {
                if (m > 0)
                {
                    l3->next = new ListNode(m);
                }
            }
        }

        return l4;
    }
};
```

### C
F
fix bug  
feilong 已提交
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
```cpp
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode *head = new ListNode(0), *r = head;
        int up = 0;
        while (l1 != NULL || l2 != NULL || up)
        {
            r->next = new ListNode(((l1 == NULL ? 0 : l1->val) + (l2 == NULL ? 0 : l2->val) + up) % 10);
            up = ((l1 == NULL ? 0 : l1->val) + (l2 == NULL ? 0 : l2->val) + up) / 10;
            r = r->next;
            if (l1 != NULL)
                l1 = l1->next;
            if (l2 != NULL)
                l2 = l2->next;
        }
        return head->next;
    }
};
```