1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/环路检测 [linked-list-cycle-lcci].html
2022-03-29 12:43:11 +08:00

45 lines
1.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给定一个链表,如果它是有环链表,实现一个算法返回环路的<code>开头节点</code>。若环不存在,请返回 <code>null</code></p>
<p>如果链表中有某个节点,可以通过连续跟踪 <code>next</code> 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 <code>pos</code> 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 <code>pos</code><code>-1</code>,则在该链表中没有环。<strong>注意:<code>pos</code> 不作为参数进行传递</strong>,仅仅是为了标识链表的实际情况。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist.png" style="height: 97px; width: 300px;" /></p>
<pre>
<strong>输入:</strong>head = [3,2,0,-4], pos = 1
<strong>输出:</strong>tail connects to node index 1
<strong>解释:</strong>链表中有一个环,其尾部连接到第二个节点。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 74px; width: 141px;" /></p>
<pre>
<strong>输入:</strong>head = [1,2], pos = 0
<strong>输出:</strong>tail connects to node index 0
<strong>解释:</strong>链表中有一个环,其尾部连接到第一个节点。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 45px; width: 45px;" /></p>
<pre>
<strong>输入:</strong>head = [1], pos = -1
<strong>输出:</strong>no cycle
<strong>解释:</strong>链表中没有环。</pre>
<p>&nbsp;</p>
<p><strong>进阶:</strong></p>
<ul>
<li>你是否可以不用额外空间解决此题?</li>
</ul>
<p>&nbsp;</p>