The issue most likely resides within this specific code line.
while(ptr!=NULL) ptr= ptr->next; //go until the end of the list
repeatedly until it matches
, which is not desirable as you dereference
immediately after the loop.
Change it to
while(ptr->next != NULL) ptr = ptr->next;
Attempt to determine if it is effective. Keep in mind that the aim is to locate the node that does not currently have the following node (to enable insertion).