判断链表是否有环
思路:
- 声明快慢指针
const hasCycle(head){
const fast,slow;
// 初始化快慢指针指向头节点
fast = slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next; // 快指针每次前进两步
slow = slow.next; // 慢指针每次前进一步
if(fast == slow){
return true;
}
}
return false;
}