# 两数相加 以下错误的选项是? ## aop ### before ```cpp #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 ```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; } ``` ## 答案 ```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; } }; ``` ## 选项 ### A ```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 ```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 ```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; } }; ```