LC 141. 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 === 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
};
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"