double free or corruption (out): 0x0000000001424780 ***


  • 0
    S

    Hi, every one, i solution is as follow.When submit, i get a run time error. But I run it on my pc use the leetcode input, it run ok. Any one help, thank you!

    struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
    {
    struct ListNode *p = l1;
    struct ListNode *q = l2;

    struct ListNode *head = malloc(sizeof(struct ListNode));
    struct ListNode *r    = head;
    r->val = 0;
    r->next=NULL;
    
    int len1 =0;
    int len2 =0;
    
    // get len
    for(;p != NULL; len1++,p=p->next); 
    for(;q != NULL; len2++,q=q->next);
    
    if (len1>len2)
    {
        p = l1; 
        q = l2;
    }else{
        p = l2;
        q = l1;
    }
    
    while(p != NULL)
    {
           r->val += p->val;
           if (q != NULL) 
           {
             r->val += q->val;     
             q = q->next;
           }
    
    
           if (r->val > 9)
           {
                r->next = malloc(sizeof(struct ListNode));
                r->next->val = 1;
                r->next->next = NULL;
                r->val = r->val - 10;
           }
        
           p  = p->next;
           if (p){
                if (r->next==NULL)
                {
                    r->next = malloc(sizeof(struct ListNode));
                    r->next->val = 0;
                }
                r = r->next;
           }
    }
    

    return head;
    }


Log in to reply
 

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