Facing Run time error during submission though same input given as custom input works fine


#1

Can some one please help me with the issue in below C++ code. I am facing run time error for some inputs during submission though the same inputs given as custom input works fine.

ListNode* Solution::deleteDuplicates(ListNode* A) {

if(!A || !A->next){
    return A;
}

ListNode *ptr = A, *temp = NULL;

while(ptr){
    //cout << ptr->val << endl;
    temp = ptr;
    while(ptr->next && ptr->val == ptr->next->val){
        ptr = ptr->next;
    }
    temp->next = ptr->next;
    free(ptr);
    ptr = temp->next;
}

return A;

}


#2

Give a try by adding an if block:
if(A==NULL){
return 0;
}
This is a boundary case!.
Hope it helps!!