My code always show Run time error on {1}, {9,9}. But I'm not sure where the problem is. Can someone help me with this?


  • 0
    X
    class Solution {
    public:
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
            int flag=0;
            ListNode *current1=l1, *current2=l2;
            ListNode *temp;
            temp=new ListNode(0);
            if(l1==NULL){
                if(l2==NULL) return NULL;
                else return l2;
            }
            while(current1->next!=NULL){
                if(current2==NULL){ 
                    if(flag==0) return l1;
                    else{
                        current1->next->val++;
                        return l1;
                    
                    }
                }
                if(current1->val+current2->val+flag<10) {
                    current1->val += current2->val+flag;
                    flag=0;
                }
                else{
                    current1->val += current2->val+flag-10;
                    flag=1;
                }
                current1=current1->next;
                current2=current2->next;
            }
            if(current2!=NULL){
                if(current1->val+current2->val+flag<10) {
                    current1->val += current2->val+flag;
                    flag=0;
                }
                else{
                    current1->val += current2->val+flag-10;
                    flag=1;
                }
                current2=current2->next;
                while(current2!=NULL){
                    if(current2->val+flag<10){
                        current2 += flag;
                        flag=0;
                    }
                    else{
                        current2 = current2+flag-10;
                        flag=1;
                    }
                    current1->next=current2;
                    current2=current2->next;
                    current1=current1->next;
                }
                if(flag==1){
                    temp->val=1;
                    current1->next=temp;
                }
            }
            else if(flag==1){
                current1->val++;
            }
            return l1;
        }
    };

  • 0
    S

    You may want to go with a simpler approach, like the code below. Readability of the code is a big factor in how you can easily debug it. If you think the solution became very complicated, you may wanna step back and look for a simpler approach ;).

    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
            
            ListNode* head = new ListNode(0);
            ListNode* current = head;
            int carry = 0;
            int sum = 0;
            
            while(l1 || l2)
            {
                sum = 0;
                if(l1)
                    sum += l1->val;
                if(l2)
                    sum += l2->val;
                if(carry)
                    sum += carry;
                    
                if(sum > 9)
                {
                    carry = 1;
                    current->val = sum%10;
                }
                else
                {
                    current->val = sum;
                    carry = 0;
                }
                
                if(l1)
    				l1 = l1->next;
    			if(l2)
    				l2 = l2->next;
    
    			if(l1 || l2 || carry)
    			{
    				current->next = new ListNode(carry);
    				current = current->next;
    			}
            }
            
            return head;
        }

Log in to reply
 

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