mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0142.环形链表II.md Java版本
This commit is contained in:
@ -186,7 +186,35 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
public class Solution {
|
||||
public ListNode detectCycle(ListNode head) {
|
||||
// 1.寻找相遇点
|
||||
ListNode fast = head;
|
||||
ListNode slow = head;
|
||||
|
||||
while (fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if (fast != slow) {
|
||||
continue;
|
||||
}
|
||||
ListNode meet = fast;
|
||||
|
||||
// 2.寻找入口点
|
||||
slow = head;
|
||||
while (slow != fast) {
|
||||
slow = slow.next;
|
||||
fast = fast.next;
|
||||
}
|
||||
|
||||
return fast;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user