Program run correctly locally but runtime error when submitted.


  • 0
    C

    Runs totally right locally, but when submitted, got runtime error, last input {1, 1}. tested it, and it works fine. What could be wrong? Thanks!

    ListNode *deleteDuplicates(ListNode *head) {

      if(head == NULL){
    	return NULL;
    }
    ListNode * ptr = head;
    ListNode * nextPtr = head->next;
    while(nextPtr)
    {
        if(ptr->val == nextPtr->val)
        {
            ListNode * temp = nextPtr;
            nextPtr = nextPtr -> next;
            ptr->next = nextPtr;
            delete(temp);
        }
        else
        {
            ptr = nextPtr;
            nextPtr = nextPtr -> next;
        }
    }  
    }

  • 0
    R

    You do not have the return statement.

    class Solution {
    public:
        ListNode *deleteDuplicates(ListNode *head) {
        
      if(!head)
        return NULL;
    
        ListNode * ptr = head;
        ListNode * nextPtr = head->next;
        while(nextPtr)
        {
            if(ptr->val == nextPtr->val)
            {
                ListNode * temp = nextPtr;
                nextPtr = nextPtr -> next;
                ptr->next = nextPtr;
                delete(temp);
            }
        else
            {
            ptr = ptr->next;
            nextPtr = nextPtr -> next;
            }
        }  
        return head;// Well....
        }
    };

  • 0
    C

    Never mind, stupid me. Forgot to return head after the while loop.
    This is ridiculous.


  • 0
    C

    Yep. Exactly the problem. Thanks :)


Log in to reply
 

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