LC 141. Linked List Cycle

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





评论和交流请发送邮件到 me@tianhegao.com





通过以下渠道赞赏此文