C 弗洛伊德';s循环检测算法,代码中是否需要if(slow_ptr!=NULL)检查
嗨,我遇到了这段实现floyds循环检测算法的代码,我想知道检查C 弗洛伊德';s循环检测算法,代码中是否需要if(slow_ptr!=NULL)检查,c,linked-list,C,Linked List,嗨,我遇到了这段实现floyds循环检测算法的代码,我想知道检查slow\u ptr=Null是必需的,因为slow\u ptr只遍历快速指针遍历或跳过的节点(即在检查它是否为非Null之后),所以检查if(fast!=Null&&fast->next!=Null)是否必要且足够 这是密码 int detectloop(struct node *list) { struct node *slow_p = list, *fast_p = list; while(slow_p &
slow\u ptr=Null
是必需的,因为slow\u ptr
只遍历快速指针遍历或跳过的节点(即在检查它是否为非Null之后),所以检查if(fast!=Null&&fast->next!=Null)
是否必要且足够
这是密码
int detectloop(struct node *list)
{
struct node *slow_p = list, *fast_p = list;
while(slow_p && fast_p &&
fast_p->next )
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p)
{
printf("Found Loop");
return 1;
}
}
return 0;
}
是的,在您发布的代码中检查
slow\u p!=NULL
是多余的