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


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;

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

return A;



Give a try by adding an if block:
return 0;
This is a boundary case!.
Hope it helps!!