Leetcode 2 两数相加

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
       
      int v1 =0 , v2 = 0,sum=0,carry=0;  
        
        ListNode* ans = new ListNode(-1);
        
        ListNode* now =ans;
        
     while( l1 || l2) {
         
         v1 = l1 ? l1->val : 0;
         v2 = l2 ? l2->val : 0;
         
         sum=v1+v2+carry;
         //更新 carry
         carry=0;
         
         if(sum >=10)  
         {
             sum-=10;
             carry=1;
             
         }
         
         now->next = new ListNode(sum);
         
         now = now->next;
         
         if(l1 != NULL)  l1=l1->next;
         if(l2 != NULL)  l2=l2->next;
     }
        //如果此时carry==1,说明还需要往前进一位
        if(carry) now->next = new ListNode(1);
        return ans->next;
        
        
    }
};

Leetcode 2 两数相加

创建一个新链表(-1->null)

carry为标识位表示进制是否加1

相关文章: