leetcode 2 consider adding two numbers overflows

First with the results then stored int error, int overflow.

Really foolish batch wailing.

Save regardless of the result, bit by bit computing directly on the line.

He describes the feeling of being misled.

/**
 * 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) {
        ListNode* head=new ListNode(0);
        ListNode * TP = head;
         int SUM = 0 ;
         BOOL with Carry = false ; // Up into a 
         the while (L1 = L2 = NULL ||!! NULL)
        {
            sum=0;
            if(l1!=NULL)
            {
                sum+=l1->val;
                l1=l1->next;
            }
            if(l2!=NULL)
            {
                sum+=l2->val;
                l2=l2->next;
            }
            if(carry)
                sum++;
            tp->next=new ListNode(sum%10);
            tp=tp->next;
            carry=sum>=10?true:false;
        }
        if(carry)
        {
            tp->next=new ListNode(1);
        }
        
        return head->next;
    }
};

 

Guess you like

Origin www.cnblogs.com/lqerio/p/11706445.html