mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-31 02:53:42 +00:00
Update 23. 链表中环的入口结点.md
题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
+15
-12
@@ -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 @@
|
||||
<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
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user