#include<stdio.h> #include<ctime> #include<random> structListNode { int val; ListNode *next; ListNode() : val(0), next(NULL) {} ListNode(int x) : val(x), next(NULL) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; classSolution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2){ auto a = l1; auto b = l2; ListNode * result = newListNode(0); ListNode * head = result; int carry = 0; while (a || b || carry) { auto sum = 0; if (a) { sum += a->val; a = a->next; } if (b) { sum += b->val; b = b->next; } sum += carry; carry = sum / 10; result->val = sum % 10; if (a || b || carry) { result->next = newListNode(0); result = result->next; } } return head; } };