检测链表中的循环
如何仅使用单个指针来检测链表中的循环? (不想要慢指针和快指针(兔子和乌龟))
How to detect loop in a linked list using only single pointer? (Don't want slow and fast pointers(Hare and Tortoise))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不介意额外的 O(N) 内存,您可以使用 hashtable 来存储沿着链表前进时访问过的节点。
在每个节点上,您检查该节点是否已存在于哈希表中。如果是这样,您就发现了一个循环。否则,您将其添加到哈希表中并移至下一个节点。
You could use a hastable to store visited nodes as you go forward along the linked list, if you don't mind the extra O(N) memory.
At each node you check whether the node already exists in the hashtable. If it does, you have found a loop. Otherwise you add it to the hashtable and move on to the next node.
Brent 算法 显然是最好的:对于无循环列表,它只需访问列出一次,而弗洛伊德的龟兔赛跑则需要重新访问一半的节点。对于带有循环的列表,它在循环中“旋转”的时间永远不会比 Floyd 长;在最好的情况下只需要一个周期,而弗洛伊德则需要多个周期。考虑一个长的无循环序列,后面跟着一个长度为 1 的循环。在这种情况下,Brent 的最佳情况是在一次循环访问后检测循环 - 因此每个节点恰好访问一次;而Floyd平均需要访问每个节点3次。
基本思想是访问链表并将当前节点的指针与一个已经访问过的节点进行比较。也就是说,每个访问节点进行一次指针比较。指针按照简单的逻辑不时更新。
Brent's algorithm is clearly the best here: For loop-free lists it simply visits the list once while Floyd's tortoise and hare requires to revisit half of the nodes. For lists with loops it never "rotates" longer in the loop than Floyd ; and in the best case needs only one cycle, when Floyd needs many. Think of a long loop-free sequence followed by a loop of lenght 1. In this case, the best case for Brent would be to detect the cycle after one visit of the loop - so visiting each node exactly once ; whereas Floyd has to visit each node on average three times.
The basic idea is to visit the linked list and compare the pointer of the current node with one already visited node. That is, one pointer comparison per visited node. The pointer is updated from time-to-time following a simple logic.
如果您的节点由值和指向下一个节点的指针组成,则可以使用列表创建指向第一个节点的指针。您可以在每个节点上检查指针是否与指向第一个节点的指针具有相同的值。
If your nodes are composed of value and a pointer to next node, you can use the list creating a pointer to the first node. You can check, on every node, if pointer has the same value than pointer to first node.