/*
 * @lc app=leetcode.cn id=141 lang=javascript
 *
 * [141] 环形链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
  // 1. hash 表实现
  // if (head === null) return false
  // const hash = new Map()
  // let cur = head;
  // while (cur) {
  //   if (hash.has(cur)) {
  //     return true;
  //   }

  //   hash.set(cur, cur)
  //   cur = cur.next;
  // }
  // return false;

  // 2. 快慢指针
  // 如果为空或者只有一个节点,则不存在环
  // if (head === null || head.next === null) return false;
  // let short = head;
  // let fast = head.next;

  // // 终止条件1:快慢指针相遇,返回true
  // while (short !== fast) {
  //   // 终止条件2:快指针null,代表遍历到头,返回false
  //   if (fast === null || fast.next === null) return false
  //   short = short.next;
  //   fast = fast.next.next;
  // }
  // return true

  // 3. 快慢指针另一种解法
  let slow = head;
  let fast = head;
  while (fast !== null && fast.next !== null) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) return true;
  }
  return false;
};
// @lc code=end