【链表】LeetCode 142. 环形链表 II
题目链接
思路
代码
class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null){
return null;
}
ListNode slow = head;
ListNode fast = head;
do{
slow = slow.next;
fast = fast.next;
if(fast != null){
fast = fast.next;
}
}while(slow != null && fast != null && slow != fast);
if(fast == null){
return fast;
}
fast = head;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}