链表的题就是找数学关系
https://leetcode-cn.com/problems/linked-list-cycle/
代码如下:
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
if(!head) return false;
let slow = head, fast = head;
while(true) {
if(fast && fast.next) fast = fast.next.next;
else return false;
slow = slow.next;
if(slow === fast) return true;
}
};