142. 环形链表 II
难度:Medium
掌握程度:Low
弗洛伊德算法,也就是快慢指针相遇之后,把快指针放到头部,满指针继续走,直到相遇
var detectCycle = function(head) {
if(head === null){
return null;
}
let slow = head;
let fast = head;
let isCycle = false;
while(fast.next !== null && fast.next.next !== null){
slow = slow.next;
fast = fast.next.next;
if(slow === fast){
isCycle = true;
break
}
}
#如果没有环形结构,那么return null
if(!isCycle){
return null;
}
fast = head;
while(slow !== fast){
slow = slow.next;
fast = fast.next;
}
return fast;
};