bool isduplicate
if(p==nullptr) {
p->next=q;
q->value=x;
}
while (p!=nullptr) {
p=p->next;
}
//arrived at NULL ptr
p->next=q;
q->value=x;
q->next=nullptr;
return q;
}
最佳答案
逻辑是错误的。在while循环开始时,current不等于跑步者,而while循环内的任何事物都不会使它们彼此相等。最终,您将取消引用空指针。
正确的逻辑比您拥有的逻辑简单。尝试这个
current = p;
while (current != nullptr)
{
runner = current->next;
while (runner != nullptr)
{
if (runner->value == current->value)
return true;
runner = runner->next;
}
current = current->next;
}
return false;
关于c++ - 检查链接列表中的重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19829929/