Leetcode题解之 —— 环形链表

思路


追及问题

  • 快慢指针

题解


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
// TODO 追击问题

let [slow, fast, flag] = [head, head, false];

while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;

if (fast == slow) {
flag = true;
break;
}
}

return flag;
};