Why does my code meet runtime error when I summit solution?


  • 0
    K

    My C code is here:
    I can successfully run my code when I click "Run Code". But it fails when I "Summit Solution".
    How do I solve this problem?
    Thanks!

    struct ListNode* oddEvenList(struct ListNode* head) {
    struct ListNode *currNode = head, *OddListHead = NULL, *OddListTail = NULL, *EvenListHead = NULL, *EvenListTail = NULL;
    
    if (head == NULL)
        return NULL;
    
    while (currNode != NULL)
    {
        if (currNode->val % 2 == 0)
        {
            if (EvenListHead == NULL)
            {
                EvenListHead = currNode;
                EvenListTail = currNode;
            }
            else
            {
                EvenListTail->next = currNode;
                EvenListTail = currNode;
            }
            currNode = currNode->next;
            EvenListTail->next = NULL;
        }
        else
        {
            if (OddListHead == NULL)
            {
                OddListHead = currNode;
                OddListTail = currNode;
            }
            else
            {
                OddListTail->next = currNode;
                OddListTail = currNode;
            }
            currNode = currNode->next;
            OddListTail->next = NULL;
        }
        
    }
    OddListTail->next = EvenListHead;
    head = OddListHead;
    return head;
    }

  • 0
    K

    I find out why my code meets runtime error. it's because of the code below.

    OddListTail->next = EvenListHead;
    

    If OddListTail == NULL, OddListTail->next cannot be accessible. Need to check the pointer OddListTail.

    BTW, I misunderstand the meaning of this question. So I fix it.

    Now my new answer is accepted.


Log in to reply
 

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