Runtime Error showing -need help


The output of the custom input is correct but it showing runtime when submitting for the same input

ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode *head=A;

while (head!=NULL && head->next!=NULL){
    ListNode *temp=head;   
    while( temp->next!=NULL && temp->val==temp->next->val){
    delete temp;

return A;



Take an example 1->2 ,now when you enter while loop your temp points 1.when head->next=temp->next
head points to 2 and temp is still pointing to 1. Now by doing head=head->next ,head pointing to NULL and you had deleted the temp , so node 1 is deleted .You are returning A which is still pointing to 1 ,since you had deleted the node 1 leading to runtime error.
first mistake is there is no need to delete any node.