C version.Runtime error.Can't figure out.


  • 0
    S
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    //stupid coding.
    bool flag=false;
    bool first=true;
    struct ListNode* current=NULL;
    struct ListNode* res;
    struct ListNode* formmer;
    do{
    	struct ListNode* tmp=(struct ListNode*)malloc(sizeof(struct ListNode));
    	tmp->next=NULL;
        formmer=current;
        current=tmp;
        if(formmer!=NULL)formmer->next=current;
        if(first){
            res=current;
            first=false;
        }
        current->val=(l1==NULL?0:l1->val)+(l2==NULL?0:l2->val)+(flag?1:0);
        if(current->val>=10){
            current->val-=10;
            flag=true;
        }else flag=false;
        l1=(l1!=NULL?l1->next:NULL);
        l2=(l2!=NULL?l2->next:NULL);
    }while(flag||(l1!=NULL || l2!=NULL));
    return res;
    

    }


Log in to reply
 

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