Skip to content

Commit 9b0ffda

Browse files
committed
三刷141
1 parent fc74cc7 commit 9b0ffda

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

docs/0141-linked-list-cycle.adoc

+49-33
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
[#0141-linked-list-cycle]
2-
= 141. Linked List Cycle
2+
= 141. 环形链表
33

4-
{leetcode}/problems/linked-list-cycle/[LeetCode - Linked List Cycle^]
4+
https://leetcode.cn/problems/linked-list-cycle/[LeetCode - 141. 环形链表 ^]
55

6+
给你一个链表的头节点 `+head+` ,判断链表中是否有环。
67

7-
Given a linked list, determine if it has a cycle in it.
8+
如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(索引从 0 开始)。*注意:`pos` 不作为参数进行传递*。仅仅是为了标识链表的实际情况。
89

9-
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-
12-
*Example 1:*
13-
14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* head = [3,2,0,-4], pos = 1
17-
*Output:* true
18-
*Explanation:* There is a cycle in the linked list, where tail connects to the second node.
19-
----
10+
_如果链表中存在环_ ,则返回 `true` 。 否则,返回 `false`
2011

2112

13+
*示例 1:*
2214

2315
image::images/0141-00.png[{image_attr}]
2416

25-
*Example 2:*
26-
27-
[subs="verbatim,quotes,macros"]
28-
----
29-
*Input:* head = [1,2], pos = 0
30-
*Output:* true
31-
*Explanation:* There is a cycle in the linked list, where tail connects to the first node.
32-
----
33-
17+
....
18+
输入:head = [3,2,0,-4], pos = 1
19+
输出:true
20+
解释:链表中有一个环,其尾部连接到第二个节点。
21+
....
3422

23+
*示例 2:*
3524

3625
image::images/0141-01.png[{image_attr}]
3726

38-
*Example 3:*
39-
40-
[subs="verbatim,quotes,macros"]
41-
----
42-
*Input:* head = [1], pos = -1
43-
*Output:* false
44-
*Explanation:* There is no cycle in the linked list.
45-
----
27+
....
28+
输入:head = [1,2], pos = 0
29+
输出:true
30+
解释:链表中有一个环,其尾部连接到第一个节点。
31+
....
4632

33+
*示例 3:*
4734

4835
image::images/0141-03.png[{image_attr}]
4936

37+
....
38+
输入:head = [1], pos = -1
39+
输出:false
40+
解释:链表中没有环。
41+
....
42+
43+
*提示:*
5044

51-
*Follow up:*
45+
* 链表中节点的数目范围是 `+[0, 10+`^`+4+`^`+]+`
46+
* `+-10+`^`+5+`^`+<= Node.val <= 10+`^`+5+`^
47+
* `+pos+``+-1+` 或者链表中的一个 *有效索引*
5248
53-
Can you solve it using _O(1)_ (i.e. constant) memory?
49+
**进阶:**你能用 `+O(1)+`(即,常量)内存解决此问题吗?
5450

5551

5652
== 思路分析
@@ -70,15 +66,35 @@ image::images/0141-08.png[{image_attr}]
7066

7167

7268
[[src-0141]]
69+
[tabs]
70+
====
71+
一刷::
72+
+
73+
--
7374
[{java_src_attr}]
7475
----
7576
include::{sourcedir}/_0141_LinkedListCycle.java[tag=answer]
7677
----
78+
--
7779
80+
二刷::
81+
+
82+
--
7883
[{java_src_attr}]
7984
----
8085
include::{sourcedir}/_0141_LinkedListCycle_2.java[tag=answer]
8186
----
87+
--
88+
89+
三刷::
90+
+
91+
--
92+
[{java_src_attr}]
93+
----
94+
include::{sourcedir}/_0141_LinkedListCycle_3.java[tag=answer]
95+
----
96+
--
97+
====
8298

8399
== 参考资料
84100

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ endif::[]
4545
|{doc_base_url}/0015-3sum.adoc[题解]
4646
|✅ 双指针
4747

48+
|{counter:codes2503}
49+
|{leetcode_base_url}/linked-list-cycle/[141. 环形链表^]
50+
|{doc_base_url}/0141-linked-list-cycle.adoc[题解]
51+
|✅ 快慢指针
52+
4853
|===
4954

5055
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0141_LinkedListCycle_2.java

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public class _0141_LinkedListCycle_2 {
4646
// tag::answer[]
4747
/**
4848
* 快慢指针
49+
*
50+
* @author D瓜哥 · https://www.diguage.com
51+
* @since 2024-07-02 21:00:01
4952
*/
5053
public boolean hasCycle(ListNode head) {
5154
if (head == null || head.next == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0141_LinkedListCycle_3 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-03-06 21:23:36
10+
*/
11+
public boolean hasCycle(ListNode head) {
12+
ListNode slow = head, fast = head;
13+
while (fast != null && fast.next != null) {
14+
slow = slow.next;
15+
fast = fast.next.next;
16+
if (slow == fast) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
22+
// end::answer[]
23+
}

0 commit comments

Comments
 (0)