检测链表中的循环

发布于 2024-12-09 02:18:41 字数 46 浏览 0 评论 0原文

如何仅使用单个指针来检测链表中的循环? (不想要慢指针和快指针(兔子和乌龟))

How to detect loop in a linked list using only single pointer? (Don't want slow and fast pointers(Hare and Tortoise))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

递刀给你 2024-12-16 02:18:41

如果您不介意额外的 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.

萌吟 2024-12-16 02:18:41

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.

恋你朝朝暮暮 2024-12-16 02:18:41

如果您的节点由值和指向下一个节点的指针组成,则可以使用列表创建指向第一个节点的指针。您可以在每个节点上检查指针是否与指向第一个节点的指针具有相同的值。

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.

太阳男子 2024-12-16 02:18:41
class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if not head:
            return False
        current_node = head
        seen = set()
        while (current_node not in seen):
            # This means there is a tail
            if current_node.next == None:
                return False
            seen.add(current_node)
            current_node = current_node.next
        # current_node would be the start of the cycle
        return True
class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if not head:
            return False
        current_node = head
        seen = set()
        while (current_node not in seen):
            # This means there is a tail
            if current_node.next == None:
                return False
            seen.add(current_node)
            current_node = current_node.next
        # current_node would be the start of the cycle
        return True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文