【发布时间】:2020-02-01 15:49:44
【问题描述】:
下面是使用 Floyd 的慢速算法发现列表中存在循环后的代码。
我们如何确定 begin 和 tortoise 会在循环开始时相遇?
Node begin = head;
tortoise = tortoise.next;
while (begin != tortoise) {
begin = begin.next;
if (tortoise.next == begin) { // Find the position of the loop and mark the node as null
tortoise.next = null;
return;
}
tortoise = tortoise.next;
}
任何帮助将不胜感激!
【问题讨论】:
-
你问的是this吗?
-
@pablo 是的,但是解释不是很清楚。
标签: java algorithm floyd-cycle-finding