判断是否有环存在:快慢指针
js实现:
var hasCycle = function(head) {
// 快指针每次行2步
let fastP = head;
let slowP = head;
while (fastP && fastP.next) {
slowP = slowP.next;
fastP = fastP.next.next;
// 移动后判断
if (slowP === fastP) return true;
}
return false;
};
判断是否有环存在:快慢指针
js实现:
var hasCycle = function(head) {
// 快指针每次行2步
let fastP = head;
let slowP = head;
while (fastP && fastP.next) {
slowP = slowP.next;
fastP = fastP.next.next;
// 移动后判断
if (slowP === fastP) return true;
}
return false;
};