AcWing 34. 链表中环的入口结点
原题链接
中等
作者:
zchuber
,
2021-04-22 21:28:51
,
所有人可见
,
阅读 249
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class Solution {
public ListNode entryNodeOfLoop(ListNode head) {
ListNode slow=head,fast=head;
while(fast!=null&&slow!=null){
slow=slow.next;
fast=fast.next;
if(fast!=null){
fast=fast.next;
if(fast==null){
return null;
}
}
if(fast==slow){
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return fast;
}
}
return null;
}
}