代码正在创建分段错误

时间:2017-01-04 14:23:17

标签: c++ linked-list segmentation-fault

/*
  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的练习题。它说编译器消息是分段错误。

0 个答案:

没有答案