leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0002.cpp (558B)


      1 class Solution {
      2   public:
      3     ListNode *addTwoNumbers(ListNode *list1, ListNode *list2) {
      4         ListNode *head, *t;
      5         t = head = new ListNode();
      6 
      7         int add = 0;
      8         while (list1 || list2 || add) {
      9             if (list1) {
     10                 add += list1->val;
     11                 list1 = list1->next;
     12             }
     13             if (list2) {
     14                 add += list2->val;
     15                 list2 = list2->next;
     16             }
     17             t = t->next = new ListNode(add % 10);
     18             add /= 10;
     19         }
     20 
     21         return head->next;
     22     }
     23 };