如何找到其中有环的链表的长度?

发布于 2024-08-31 01:50:26 字数 154 浏览 4 评论 0原文

这是面试时提出的问题之一。如何找到其中有循环的链表的长度。我知道如何使用兔子和乌龟技术来计算链表是否有循环。我什至知道如何通过将地址存储在哈希集中来计算长度。算法的运行时间应该是 O(n)。

但我无法告诉的是如何在不使用 O(n) 外部空间的情况下计算链表的长度。请帮我。谢谢。

This was one of the interview questions asked. How to find the length of a linked list that is having cycle in it. I know how to calculate whether a linked list has a cycle or not using Hare and Tortoise technique. I even know how to calculate the length by storing the addresses in a hashset. The running time of the Algorithm should be O(n).

But what I was not able to tell is how to calculate the length of the linked list without using a external space of O(n). Please help me. Thank you.

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

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

发布评论

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

评论(6

2024-09-07 01:50:26

我认为如果你知道循环的起始节点,你就可以知道循环的长度,从而知道链表中的节点总数。

为了获得起点,您只需要 O(1) 空间。

假设你的两个指针,快指针和慢指针在“节点 t”处相遇。

增加一个指针以指向下一个节点。
另一个指针指向链表的开头。
现在增加两个指针直到它们相遇。

交汇点是循环的起始节点。

由此,您可以通过再次遍历来获取循环长度,因为您知道循环的起点。

I think If some how you know the starting node of cycle , you can know the length of cycle and hence the total number of nodes in linked list.

For getting starting point you need only O(1) space.

Suppose your two pointers,fast and slow met at 'node t'.

increment one pointer to point to next node.
and the other pointer to start of linked list.
Now increment both the pointers until they meet.

The meeting point is starting node of cycle.

From this you can get the Length of cycle by traversing again since you know the starting point of cycle.

静若繁花 2024-09-07 01:50:26

检测到循环后,您需要计算循环的长度及其开始的位置。这些的总和就是列表中不同节点的总数。详细信息例如:http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare

Once you've detected the cycle, you need to calculate the length of the cycle, and the position at which it starts. The sum of these is the total number of distinct nodes in the list. Details for example here: http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare

笔落惊风雨 2024-09-07 01:50:26
  1. 应用龟兔赛跑算法并当字符相遇时停止
  2. 继续仅使用Tortoise迭代列表并反转它将经过的每个链接

反向链接的数量就是循环的长度

  1. Apply Tortoise and Hare algorithm and stop when characters meet
  2. Continue iterating through the list only with Tortoise and reverse each link it will pass

The number of reversed links is the length of the cycle

我喜欢麦丽素 2024-09-07 01:50:26

检测循环点
通过(慢指针和快指针的方法),找到环路检测节点

现在取指针P1和P2,P1在环路检测节点,P2从链表头开始
一次遍历两个指针

两个指针在链表中出现循环的节点处相遇

使用标准的快慢指针算法找到循环检测点

在循环检测点取两个指针 P1 和 P2。将指针 P1 放在同一位置,然后将 P2 向前移动一步。重复直到两个指针相遇。为每次迭代保留一个递增的计数变量,该变量给出了循环的长度。假设长度为 L1

再次取两个指针 P1 和 P2。 P1位于链表头部,P2位于环路检测点。将两个指针一次向前推进一步。重复直到两个指针相遇。这个过程相当于我们用来计算链表中导致循环的节点的过程。为每次迭代保留一个递增的计数变量,该变量给出了直到合并点的列表长度。假设长度为 L2
现在包含循环的列表的长度是L1+L2

Detect loop point
By (Slow pointer and fast pointer approach), find the loop detection node

Now take pointers P1 and P2, P1 at the loop detection node and P2 starting from head of the linked list
Traverse both the pointers one at a time

Both the pointers meet at the node because of which there is a loop in the linked list

Use the standard fast and slow pointer algorithm find the loop detection point

Take two pointers P1 and P2 at loop detection point. Place pointer P1 at the same place and move P2 forward one step at a time. Repeat until both the pointers meet together. Keep a count variable incremented for every iteration which gives length of the loop. Let say the length is L1

Again take two pointers P1 and P2. P1 at the head of the linked list and P2 at the loop detection point. Forward both the pointers one step at a time. Repeat until both the pointers meet together. This procedure is equivalent to the one we use to calculate node that causes loop in a linked list. Keep a count variable incremented for every iteration which gives the length of the list until the merging point. Let say the length is L2
Now the length of the list that contains loop is L1+ L2

时光礼记 2024-09-07 01:50:26

以下是流行的兔子和乌龟算法用于查找链表长度的正确性的数学证明。

兔子和乌龟的数学

Here's the mathematical justification of the correctness of the popular hare and tortoise algorithm for finding the length of the linked list.

Math of the hare and the tortoise

偷得浮生 2024-09-07 01:50:26

不会陷入无限循环吗?如果 1 2 3 ... 9 10 形成一个循环,从 1 开始。假设当起始节点的指针到达 1 时,另一个指针在节点 8 处。那么它们的移动如下
指针1:- 1 2 3 4 5 ..
指针2:- 8 9 10 1 2..
显然他们永远不会平等。

wont it go in infinite loop? if 1 2 3 ... 9 10 form a loop ,which starts at 1.Suppose when pointer from start node reaches 1 ,other pointer is at node 8. then they move as
pointer1:- 1 2 3 4 5 ..
pointer2:- 8 9 10 1 2..
clearly they wont ever be equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文