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
|
class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int a = 0, b = 0, c = 0; ListNode *n1 = l1, *n2 = l2, dummy_head(-1), *t = &dummy_head; while (n1 || n2 || c > 0) { a = n1 ? n1->val : 0; b = n2 ? n2->val : 0; int s = a + b + c; t->next = new ListNode(s % 10); t = t->next; c = s / 10; n1 = n1 ? n1->next : n1; n2 = n2 ? n2->next : n2; } return dummy_head.next; } };
|