From 8a47dbbb952fba48919edeb615e2a8277a7cb4c5 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:04 +0800 Subject: [PATCH] =?UTF-8?q?Update=2023.=20=E9=93=BE=E8=A1=A8=E4=B8=AD?= =?UTF-8?q?=E7=8E=AF=E7=9A=84=E5=85=A5=E5=8F=A3=E7=BB=93=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/23. 链表中环的入口结点.md | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/notes/23. 链表中环的入口结点.md b/notes/23. 链表中环的入口结点.md index 64c6f21b..4e38045f 100644 --- a/notes/23. 链表中环的入口结点.md +++ b/notes/23. 链表中环的入口结点.md @@ -1,6 +1,6 @@ # 23. 链表中环的入口结点 -[NowCoder](https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/linked-list-cycle-ii/) ## 题目描述 @@ -25,19 +25,22 @@

```java -public ListNode EntryNodeOfLoop(ListNode pHead) { - if (pHead == null || pHead.next == null) +public ListNode detectCycle(ListNode head) { + if (head == null || head.next == null) return null; - ListNode slow = pHead, fast = pHead; - do { + ListNode slow = head, fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; fast = fast.next.next; - slow = slow.next; - } while (slow != fast); - fast = pHead; - while (slow != fast) { - slow = slow.next; - fast = fast.next; + if (slow == fast) { + fast = head; + while (slow != fast) { + slow = slow.next; + fast = fast.next; + } + return slow; + } } - return slow; + return null; } ```