leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0002.cpp (558B)
0 class Solution { 1 public: 2 ListNode *addTwoNumbers(ListNode *list1, ListNode *list2) { 3 ListNode *head, *t; 4 t = head = new ListNode(); 5 6 int add = 0; 7 while (list1 || list2 || add) { 8 if (list1) { 9 add += list1->val; 10 list1 = list1->next; 11 } 12 if (list2) { 13 add += list2->val; 14 list2 = list2->next; 15 } 16 t = t->next = new ListNode(add % 10); 17 add /= 10; 18 } 19 20 return head->next; 21 } 22 };