floyd算法 每一层循环_链接列表循环检测– Floyd的循环查找算法

floyd算法 每一层循环

In this tutorial, we’ll be discussing a very popular algorithm which is used to detect whether a LinkedList has loops. It’s called Floyd’s Cycle-Finding Algorithm.

在本教程中,我们将讨论一种非常流行的算法,该算法用于检测LinkedList是否具有循环。 它被称为弗洛伊德的循环发现算法。

LinkedList循环检测 (LinkedList Loop Detection)

A loop exists in a LinkedList when no NULL is reached as we traverse throughout the LinkedList.

当遍历整个LinkedList时没有到达NULL时,LinkedList中存在一个循环。

So in order to detect whether a LinkedList has a loop or not, we can traverse through the LinkedList and add each Node to the HashSet of visited notes if it’s been visited for the first item.
Once we reach a node that’s already present in the HashSet we can tell that there was a loop.

因此,为了检测LinkedList是否具有循环,我们可以遍历LinkedList,并将每个Node添加到已访问笔记的HashSet(如果已访问第一项)。
一旦我们到达HashSet中已经存在的节点,我们就可以知道存在一个循环。

But this approach, though simpler takes up extra space.

但是这种方法虽然更简单,但会占用额外的空间。

Another way is to set a flag for the visited nodes in the LinkedList Node data itself. But again, this approach would take some additional space. More than what we used earlier.

另一种方法是在LinkedList节点数据本身中为访问的节点设置标志。 但是同样,这种方法会占用一些额外的空间。 比我们之前使用的更多。

Hence, the ideal approach to detect a loop is using Floyd’s Cycle-Finding Algorithm

因此,检测环路的理想方法是使用弗洛伊德的循环查找算法

弗洛伊德的循环发现算法 (<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值