马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/**
* 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) {
ListNode slow = head;
ListNode fast = head;
ListNode end = null;
while(slow != null && fast != null){
fast = fast.next;
if(fast == null){
return null;
}
slow = slow.next;
fast = fast.next;
if(slow == fast){
end = slow;
break;
}
}
while(end != null && head != null){
if(end == head){
return head;
}
end = end.next;
head =head.next;
}
return null;
}
}
|