# 两数相加

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

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

示例 1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:
[7,0,8]
解释:
342 + 465 = 807.

示例 2:

输入:l1 = [0], l2 = [0]
输出:
[0]

示例 3:

输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:
[8,9,9,9,0,0,0,1]

提示:

以下错误的选项是?

## aop ### before ```c #include #include #include #include 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 ```c 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; } ``` ## 答案 ```c 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; } }; ``` ## 选项 ### A ```c 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 ```c 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 ```c 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; } }; ```