我试图从链表中删除重复项,因此如果链表以 [1,1,2,3,4,4,4,5] 开头,那么附加链表将是 [1,2,3 ,4,5]。代码如下。
struct node_h
{
int data;
struct node_h* next;
} node;
void remove_h(node* head)
{
while (head != NULL)
{
if (head->data == head->next->data)
{
if (head->next->next == NULL)
{
head->next = NULL;
}
else
{
head->next = head->next->next;
}
}
head = head->next;
}
}
问题在于它的段错误。有时。
最佳答案
罪魁祸首是 if (head->data == head->next->data)
... 如果 head->next
为空,这一定是段错误.
首先检查这个条件,没有重复是可能的,如果它是真的:只需添加 if (head->next == NULL) break;
作为 while 中的第一个语句或修改while 条件。
关于c - 从 c 中的链表中删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671689/