/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return bool布尔型
* 在这个代码中,我们使用两个指针,一个快指针(fast)每次前进两步,一个慢指针(slow)
* 每次前进一步。如果链表中有环,快指针最终会在某个时刻追上慢指针,从而返回true表示存在环。
* 如果链表没有环,快指针将会达到链表的末尾,循环结束,返回false表示无环。
*
*/
function hasCycle( head ) {
// write code here
if(!head || !head.next){
return false
}
let slow = head
let fast = head
while(fast && fast.next){
slow = slow.next
fast = fast.next.next
if(slow === fast){
return true
}
}
return false
}
module.exports = {
hasCycle : hasCycle
};