My answer is same with expected answer, but submit code failed.


  • 0
    W

    As per title.

    Error info:
    Submission Result: Runtime Error
    Last executed input:[4,4,0,1,7,4,5,5,3,1]
    [5,2,4,3,0,7,6,1,0,2]

    I've try to this input and run my code, my anser and expected anser are exactly the same. But submit code failed with above error info.

    My code as below:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        unsigned int more = 0;
        struct ListNode *ret_node = NULL;
        struct ListNode *node = NULL;
        struct ListNode *pre_node = NULL;
        
        if ((NULL == l1) && (NULL == l2)) {
            return NULL;
        }
        
        if (NULL == l1) {
            return l2;
        }
        
        if (NULL == l2) {
            return l1;
        }
        
        while (l1 && l2) {
            node = (struct ListNode*)malloc(sizeof(struct ListNode));
            if (NULL == node)
                return NULL;
            
            node->val = (l1->val + l2->val + more)%10;
            more = (l1->val + l2->val + more)/10;
                
            if (pre_node) {
                pre_node->next = node;
            }
                
            if (!ret_node) {
                ret_node = node;
            }
            
            pre_node = node;
            l1 = l1->next;
            l2 = l2->next;
        } 
        
        if (l1) {
            l1->val += more;
            pre_node->next = l1;
            return ret_node;
        }
        
        if (l2) {
            l2->val += more;
            pre_node->next = l2;
            return ret_node;
        }
        
        return ret_node;
    }
    

  • 0

    Could you please format your code nicely by clicking on edit (with the pencil icon beside), and then indent your entire code block with 4 spaces? You can do that by selecting your entire code, and then click on the code icon above the editor to indent the block by 4 spaces. Try looking for the code icon which looks like this -> {}.


  • 0

    The reason the Run Code result is inconsistent with Submit Solution result is due to your code has undefined behavior, as documented in the FAQ.

    The runtime error is due to you not initializing node->next to NULL after calling malloc. malloc does not initialize the member data of the struct, it just provides a pointer to the raw memory.

    After changing:

    node = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (NULL == node)
        return NULL;
    

    to:

    node = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (NULL == node)
        return NULL;
    node->next = NULL;
    

    Your solution now gets Wrong Answer.


  • 0
    W

    Thank very much.
    Previous issue fixed. Now it is my solution problem which lead to submitting failed.


  • 0
    W

    I did not know the code format will cause submitting fail issue. Thanks for your reminder.


Log in to reply
 

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