解题思路:
分别设置2个指针(s,q)指向链表的头部,s每次指向下面一个(s = s.next),q每次指向下面2个(q = q.next.next).
如果存在环,q总会在某一时刻追上s
/*** Definition for singly-linked list.* function ListNode(val) {* this.val = val;* this.next = null;* }*//*** @param {ListNode} head* @return {boolean}*/
var hasCycle = function(head) {let s = head;let f = head;while(f && f.next){s = s.next;f = f.next.next;if(s === f) return true}return false;
};