代码逻辑中比较重要一点就是快指针的回调判断
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
function hasCycle( head ) {
let flag = false;
const check = function(s, f) {
if (f && f.next) { // 这里只用判断快指针即可
s = s.next;
f = f.next.next;
s === f ? flag = true : check(s, f);
}
}
check(head, head);
return flag;
}
module.exports = {
hasCycle : hasCycle
};
京公网安备 11010502036488号