Hit run-time error when submitting the code but it works fine when run test cases.


  • 0
    S

    Hi,
    When I submit my codem an run-time error as below raised. The the code works fine when I try "Run Code",
    It also run well on my local linux server. Can you tell me why this runtime error happens?

    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        int sum;
        int prom = 0;
        struct ListNode *res = NULL;
        struct ListNode *head = NULL;
        struct ListNode *prev = NULL;
    
        while (l1 || l2) {
            sum = 0;
            if (l1) {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2) {
                sum += l2->val;
                l2 = l2->next;
            }
            sum += prom;
            if (sum >= 10) {
                prom = 1;
                sum -= 10;
            } else {
                prom = 0;
            }
            res = (struct ListNode *)malloc(sizeof(struct ListNode));
            bzero(res, sizeof(struct ListNode));
            res->val = sum;
            printf("res->val=%d, sizeof(res)=%d\n", res->val, sizeof(struct ListNode));
            if (!prev) {
                prev = res;
                head = res;
            } else {
                prev->next = res;
                prev = res;
            }
        }
        if (prom == 1) {
            res = (struct ListNode *)malloc(sizeof(struct ListNode));
            res->val = 1;
            prev->next = res;
        }
        return head;
    }
    

Log in to reply
 

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