LeetCode 141. 环形链表

Posted by cody1991 on August 2, 2020

简单思路:这题其实也很简单,一个快慢指针然后看是否有相同的情况。(另外 js 也可以用 JSON.stringify 来判断是否有环形,也可以标记每个指针,如果遍历过加个 flag ,下次再遍历到的话就是环形的)

141. 环形链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 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;
  let fast = head.next;
  let has = false;
  while (slow !== null && fast !== null && fast.next !== null) {
    if (slow === fast) {
      has = true;
      break;
    }
    slow = slow.next;
    fast = fast.next.next;
  }
  return has;
};