题目就不赘述了,直接思路和代码
代码如下
var detectCycle = function(head
) {
if(!head
||!head
.next
){
return null
}
let slow
=head
,fast
=head
while(fast
&&fast
.next
){
slow
=slow
.next
fast
=fast
.next
.next
if(slow
===fast
){
fast
=head
while(slow
!==fast
){
slow
=slow
.next
fast
=fast
.next
}
return fast
}
}
return null
};
运行结果
转载请注明原文地址:https://ipadbbs.8miu.com/read-60318.html