/*
Remove all duplicate elements from a sorted linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* RemoveDuplicates(Node *head)
{
// This is a "method-only" submission.
// You only need to complete this method.
Node *ptr= head;
if(ptr == NULL){
return head;
}
while(ptr != NULL){
if(ptr->data == (ptr->next)->data){
ptr->next = (ptr->next)->next;
}
else{
ptr = ptr->next;
}
}
return head;
}
为什么此代码会产生分段错误?这是hackerrank,fyi的练习题。它说编译器消息是分段错误。