问题描述
我需要一个方法,需要一个链表作为参数,并返回true或false,如果它是圆形或不。
I need a method that takes a linkedlist as a parameter and return true or false if it is circular or not.
例如:循环链表表示有节点pointes的任何previous节点。我忘了告诉一些约束,我不能使用任何数据结构或dynmaic内存分配。我只能用局部变量和alogrithm可以在n个步骤来完成有人对我说(我想现在使用两个指针?)
ex:circular linkedlist means there is node pointes to any previous node. I forgot to tell some constraints, I can not use any datastructure or dynmaic memory allocations.I can use local variables only and the alogrithm can be done in n steps as someone said to me (I am thinking now to use two pointers?)
推荐答案
我相信你正在寻找弗洛伊德的循环查找算法。有一个更好的解释比我可以给它在这里。
I believe you're looking for Floyd's Cycle-Finding Algorithm. There's a better explanation than I could give of it over here.
也有一对夫妇在C和方案的实施与在这里 文档。
There are also a couple of implementations in C and Scheme with documentation over here.
这篇关于检查LinkedList的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!