/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
function hasCycle(head) {
// write code here
//如果为空,则无环
if (head == null) 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
};