Update 23. 链表中环的入口结点.md

题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
CyC2018
2026-10-28 13:45:04 +08:00
parent ce1603ad31
commit 8a47dbbb95
+15 -12
View File
@@ -1,6 +1,6 @@
# 23. 链表中环的入口结点 # 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 @@
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/bb7fc182-98c2-4860-8ea3-630e27a5f29f.png" width="500"/> </div><br> <div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/bb7fc182-98c2-4860-8ea3-630e27a5f29f.png" width="500"/> </div><br>
```java ```java
public ListNode EntryNodeOfLoop(ListNode pHead) { public ListNode detectCycle(ListNode head) {
if (pHead == null || pHead.next == null) if (head == null || head.next == null)
return null; return null;
ListNode slow = pHead, fast = pHead; ListNode slow = head, fast = head;
do { while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next; fast = fast.next.next;
slow = slow.next; if (slow == fast) {
} while (slow != fast); fast = head;
fast = pHead; while (slow != fast) {
while (slow != fast) { slow = slow.next;
slow = slow.next; fast = fast.next;
fast = fast.next; }
return slow;
}
} }
return slow; return null;
} }
``` ```