C++ RuntimeError,why?


  • 1
    Y
    class Solution {
    public:
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
            int a=0,b=0;
            bool scanned1=false,scanned2=false;
            for (int i=0;;i++){
                if (!scanned1){
                    a+=l1->val*pow(10,i);
                    if(l1->next==NULL){
                        scanned1=true;
                    }
                    l1=l1->next;
                }
                if (!scanned2){
                    b+=l2->val*pow(10,i);
                    if(l2->next==NULL){
                        scanned2=true;
                    }
                    l2=l2->next;
                }
                if(scanned1&&scanned2)
                    break;
            }
            int sum=a+b;
            ListNode sumlist(sum%10);
            ListNode* psumlist=&sumlist;
            ListNode* temp=psumlist;
            while(sum!=0){
                sum=sum/10;
                temp->next=(ListNode*)malloc(sizeof(ListNode));
                temp->next->val=sum%10;
                temp->next->next=NULL;
                temp=temp->next;
            }
            return psumlist;
        }
    };

Log in to reply
 

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