Skip to content

Commit 3e05974

Browse files
committed
三刷142
1 parent 301ee7d commit 3e05974

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

docs/0142-linked-list-cycle-ii.adoc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,35 @@ image::images/0142-15.png[{image_attr}]
8888

8989

9090
[[src-0142]]
91+
[tabs]
92+
====
93+
一刷::
94+
+
95+
--
9196
[{java_src_attr}]
9297
----
9398
include::{sourcedir}/_0142_LinkedListCycleII.java[tag=answer]
9499
----
100+
--
95101
102+
二刷::
103+
+
104+
--
96105
[{java_src_attr}]
97106
----
98107
include::{sourcedir}/_0142_LinkedListCycleII_2.java[tag=answer]
99108
----
109+
--
110+
111+
三刷::
112+
+
113+
--
114+
[{java_src_attr}]
115+
----
116+
include::{sourcedir}/_0142_LinkedListCycleII_3.java[tag=answer]
117+
----
118+
--
119+
====
100120

101121

102122
== 思考题

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@
550550
|{doc_base_url}/0387-first-unique-character-in-a-string.adoc[题解]
551551
|哈希+字符串
552552

553+
|{counter:codes}
554+
|{leetcode_base_url}/linked-list-cycle-ii/[142. Linked List Cycle II^]
555+
|{doc_base_url}/0142-linked-list-cycle-ii.adoc[题解]
556+
|快慢指针
557+
553558
|===
554559

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class _0142_LinkedListCycleII_2 {
1414
// tag::answer[]
1515

1616
/**
17-
*
17+
* @author D瓜哥 · https://www.diguage.com
18+
* @since 2024-07-02 21:08:43
1819
*/
1920
public ListNode detectCycle(ListNode head) {
2021
if (head == null || head.next == null) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0142_LinkedListCycleII_3 {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-09-14 17:02:00
11+
*/
12+
public ListNode detectCycle(ListNode head) {
13+
ListNode slow = head;
14+
ListNode fast = head;
15+
while (fast != null && fast.next != null) {
16+
slow = slow.next;
17+
fast = fast.next;
18+
fast = fast.next;
19+
if (slow == fast) {
20+
break;
21+
}
22+
}
23+
if (fast == null || fast.next == null) {
24+
return null;
25+
}
26+
slow = head;
27+
while (slow != fast) {
28+
slow = slow.next;
29+
fast = fast.next;
30+
if (slow == fast) {
31+
break;
32+
}
33+
}
34+
return slow;
35+
}
36+
// end::answer[]
37+
}

0 commit comments

Comments
 (0)