添加 0142.环形链表II.md Java版本

This commit is contained in:
h2linlin
2021-05-13 20:21:03 +08:00
committed by GitHub
parent ef11a0cfdf
commit 07c3db6288

View File

@ -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