|
1 | 1 | [#0142-linked-list-cycle-ii] |
2 | | -= 142. Linked List Cycle II |
| 2 | += 142. 环形链表 II |
3 | 3 |
|
4 | | -:stem: latexmath |
| 4 | +https://leetcode.cn/problems/linked-list-cycle-ii/[LeetCode - 142. 环形链表 II ^] |
5 | 5 |
|
6 | | -{leetcode}/problems/linked-list-cycle-ii/[LeetCode - Linked List Cycle II^] |
| 6 | +给定一个链表的头节点 `head` ,返回链表开始入环的第一个节点。 _如果链表无环,则返回 `null`。_ |
7 | 7 |
|
8 | | -Given a linked list, return the node where the cycle begins. If there is no cycle, return `null`. |
| 8 | +如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(*索引从 0 开始*)。如果 `pos` 是 `-1`,则在该链表中没有环。*注意:`pos` 不作为参数进行传递*,仅仅是为了标识链表的实际情况。 |
9 | 9 |
|
10 | | -To represent a cycle in the given linked list, we use an integer `pos` which represents the position (0-indexed) in the linked list where tail connects to. If `pos` is `-1`, then there is no cycle in the linked list. |
| 10 | +*不允许修改* 链表。 |
11 | 11 |
|
12 | | -*Note:* Do not modify the linked list. |
13 | | - |
14 | | -.Example 1: |
15 | | -[source] |
16 | | ----- |
17 | | -Input: head = [3,2,0,-4], pos = 1 |
18 | | -Output: tail connects to node index 1 |
19 | | -Explanation: There is a cycle in the linked list, where tail connects to the second node. |
20 | | ----- |
| 12 | +*示例 1:* |
21 | 13 |
|
22 | 14 | image::images/0142-01.png[{image_attr}] |
23 | 15 |
|
24 | | -.Example 2: |
25 | | -[source] |
26 | | ----- |
27 | | -Input: head = [1,2], pos = 0 |
28 | | -Output: tail connects to node index 0 |
29 | | -Explanation: There is a cycle in the linked list, where tail connects to the first node. |
30 | | ----- |
| 16 | +.... |
| 17 | +输入:head = [3,2,0,-4], pos = 1 |
| 18 | +输出:返回索引为 1 的链表节点 |
| 19 | +解释:链表中有一个环,其尾部连接到第二个节点。 |
| 20 | +.... |
| 21 | + |
| 22 | +*示例 2:* |
31 | 23 |
|
32 | 24 | image::images/0142-02.png[{image_attr}] |
33 | 25 |
|
34 | | -.Example 3: |
35 | | -[source] |
36 | | ----- |
37 | | -Input: head = [1], pos = -1 |
38 | | -Output: no cycle |
39 | | -Explanation: There is no cycle in the linked list. |
40 | | ----- |
| 26 | +.... |
| 27 | +输入:head = [1,2], pos = 0 |
| 28 | +输出:返回索引为 0 的链表节点 |
| 29 | +解释:链表中有一个环,其尾部连接到第一个节点。 |
| 30 | +.... |
| 31 | + |
| 32 | +*示例 3:* |
41 | 33 |
|
42 | 34 | image::images/0142-03.png[{image_attr}] |
43 | 35 |
|
44 | | -*Follow-up:* |
| 36 | +.... |
| 37 | +输入:head = [1], pos = -1 |
| 38 | +输出:返回 null |
| 39 | +解释:链表中没有环。 |
| 40 | +.... |
45 | 41 |
|
46 | | -Can you solve it without using extra space? |
| 42 | +*提示:* |
| 43 | + |
| 44 | +* 链表中节点的数目范围在范围 `[0, 10^4^]` 内 |
| 45 | +* `-10^5^ \<= Node.val \<= 10^5^` |
| 46 | +* `pos` 的值为 `-1` 或者链表中的一个有效索引 |
| 47 | +
|
| 48 | +**进阶:**你是否可以使用 stem:[O(1)] 空间解决此题? |
47 | 49 |
|
48 | 50 | == 思路分析 |
49 | 51 |
|
@@ -116,6 +118,15 @@ include::{sourcedir}/_0142_LinkedListCycleII_2.java[tag=answer] |
116 | 118 | include::{sourcedir}/_0142_LinkedListCycleII_3.java[tag=answer] |
117 | 119 | ---- |
118 | 120 | -- |
| 121 | +
|
| 122 | +四刷:: |
| 123 | ++ |
| 124 | +-- |
| 125 | +[{java_src_attr}] |
| 126 | +---- |
| 127 | +include::{sourcedir}/_0142_LinkedListCycleII_4.java[tag=answer] |
| 128 | +---- |
| 129 | +-- |
119 | 130 | ==== |
120 | 131 |
|
121 | 132 |
|
|
0 commit comments