The result can't pass the case L1=[1] L2=[9 9],my result is[0 0]


  • 0
    Q

    class Solution {
    public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode list_head(0);
    ListNode *list_value=&list_head;
    int i=0;

    while(l1!=NULL && l2!=NULL){
        list_value->next=new ListNode(0);
        list_value=list_value->next;
    
        list_value->val=(l1->val+l2->val)%10+i;
        i=(l1->val+l2->val)/10;
    
        l1=l1->next;
        l2=l2->next;
    }
    
    if(l1==NULL  && l2!=NULL){
        list_value=list_value->next=l2;
    
        while(i>0){
            int m;
            m=i+list_value->val;
            list_value->val=m%10;
            i=m/10;
    
            list_value=list_value->next;
            if(list_value==NULL && i>0 ){
                list_value=new ListNode(0);
                list_value->next=NULL;
                break;
            }
    
        }
    }
    
     if(l2==NULL && l1!=NULL){
    
        list_value=list_value->next=l1;
        while(i>0){
            int m;
            m=i+list_value->val;
            list_value->val=m%10;
            i=m/10;
            list_value=list_value->next;
    
            if(list_value==NULL && i>0){
                list_value=new ListNode(0);
                list_value->val=i;
                break;
            }
    
        }
    
    }
    
    if(l1==NULL&&l2==NULL && i>0){
        list_value->next=new ListNode(0);
        list_value->next->val=i;
    }
    
    
    return (list_head.next);
    

    };

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.