/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || 网页链接 == null){
return null;
}
//判断有无环,快慢指针
ListNode fast = head;
ListNode slow = head;
while(fast!=null && 网页链接 != null){
slow = slow.next;
fast = fast.next.next;
if(fast ==slow){
//代表有环
//找到入口
ListNode p = head;
while(p!=slow){
p = p.next;
slow=slow.next;
}
return p;
}
}
return null;
}
}
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || 网页链接 == null){
return null;
}
//判断有无环,快慢指针
ListNode fast = head;
ListNode slow = head;
while(fast!=null && 网页链接 != null){
slow = slow.next;
fast = fast.next.next;
if(fast ==slow){
//代表有环
//找到入口
ListNode p = head;
while(p!=slow){
p = p.next;
slow=slow.next;
}
return p;
}
}
return null;
}
}










