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.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

这道并不是什么难题，算法很简单，链表的数据类型也不难，就是建立一个新链表，然后把输入的两个链表从头往后撸，每两个相加，添加一个新节点到新链表后面。

为了避免两个输入链表同时为空，我们建立一个 dummy 结点，将两个结点相加生成的新结点按顺序加到 dummy 结点之后，由于 dummy 结点本身不能变，所以用一个指针 cur 来指向新链表的最后一个结点。这道题好就好在最低位在链表的开头，所以可以在遍历链表的同时按从低到高的顺序直接相加。

while 循环的条件两个链表中只要有一个不为空行，由于链表可能为空，所以在取当前结点值的时候，先判断一下，若为空则取0，否则取结点值。然后把两个结点值相加，同时还要加上进位 carry。然后更新 carry，直接 sum/10 即可，然后以 sum%10 为值建立一个新结点，连到 cur 后面，然后 cur 移动到下一个结点。之后再更新两个结点，若存在，则指向下一个位置。while 循环退出之后，最高位的进位问题要最后特殊处理一下，若 carry 为1，则再建一个值为1的结点，代码如下：

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(-1);
ListNode *cur = dummy;

int carry = 0;

while(l1 || l2) {
int val1 = (l1 == nullptr ? 0 : l1->val);
int val2 = (l2 == nullptr ? 0 : l2->val);

int sum = val1 + val2 + carry;
carry = sum / 10;
cur->next = new ListNode(sum % 10);
cur = cur->next;

if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}

if(carry)
cur->next = new ListNode(carry);

return dummy->next;
}
};

### 2. 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 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.

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


class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(-1);
ListNode *cur = dummy;

l1 = reverseList(l1);
l2 = reverseList(l2);

int carry = 0;
while(l1 || l2) {
int val1 = (l1 == nullptr ? 0 : l1->val);
int val2 = (l2 == nullptr ? 0 : l2->val);

int sum = val1 + val2 + carry;
carry = sum / 10;
cur->next = new ListNode(sum % 10);
cur = cur->next;

if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}

if(carry)
cur->next = new ListNode(carry);

return reverseList(dummy->next);
}

private:
ListNode *pre = nullptr;

while(cur) {
ListNode *temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}

return pre;
}
};

01-18 1542

11-13 197

02-25 142

12-30 18

02-18 32

03-14 76

04-25 1321

01-05 1000

11-17 72

10-30 1323

11-20 1914

08-27 20

07-14 9

03-21 895

02-24 307

11-21 29

09-03 294

01-07 2337

11-30 9

04-14 59万+

03-13 15万+

03-04 14万+

03-08 2万+

04-25 6万+

03-10 13万+

03-10 18万+

03-12 11万+

03-13 11万+

03-18 1万+

03-19 8万+

03-19 3万+

03-22 4万+

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 10万+

03-30 16万+

05-31 6726

04-02 1万+

04-02 6104

04-02 4万+

04-06 1万+

04-06 7万+

04-09 8万+

04-09 3万+

05-17 8941

04-11 4万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

05-01 6622

05-03 1万+

05-16 5万+

05-06 1万+

05-07 3885

05-07 1万+

05-08 4万+

05-10 3515

05-14 6842

05-16 3795

05-16 1万+

05-17 5268

05-30 1003

05-20 1174

05-18 2401

05-18 8702

05-27 1662

05-18 3823

05-28 1424

05-19 1万+

05-19 2053

05-21 8228

05-21 8282

05-27 7349

05-25 5517

#### 前端很容易遇到的一个大坑

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客