循环在单个链表中 [英] loop in a single linked list
本文介绍了循环在单个链表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
在这里,我怎么能找到一个链表包含
循环。
Hi All,
here i have a daught how can i find wheather a linked list contains a
loop or not.
推荐答案
一种方法是标记你去过的元素
one way is to mark the element you visited
假设我的节点包含如下声明。
struct node
{
int info;
struct节点* next_ptr;
}
然后如何标记节点是否被访问。
请指定代码
suppose my node contains like the following declaration.
struct node
{
int info;
struct node *next_ptr;
}
then how can i mark the node is visited or not.
please specify the code
*如果链接列表是只读的,则采用两个指针方式...两个指向链接列表开头的
。现在将p1增加1,将p2增加2
并比较两者。如果他们是平等的,那就有一个循环。重复这个
直到p2指向null。
*如果你有条件不修改节点但你可以改变
链接,然后反转链表。如果你到达头节点然后
有一个周期....
*if the linked list is read only, take two pointer approach..both
pointing to beginning of linked list. now increment p1 by 1 and p2 by 2
and compare both. if they are equal there is a cycle. repeat this
untill p2 points to null.
*if u have the condition not to modify the node but u can change the
links, then reverse the linked list. if u reach the head node then
there is a cycle ....
这篇关于循环在单个链表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文