1. Title Description

Git git git git git git git git git git git git git git Returns true if there are rings in the list. Otherwise, return false.

2. How to solve the problem

You can watch my video for ideas

  • How fast a pointer
  • The fast pointer step is 2, and the slow pointer step is 1
  • If the fast pointer reaches the tail node and the two do not meet, then there is no loop, otherwise there is a loop

Three, specific code

  var hasCycle = function(head) {
    let fast = head;
    let slow = head;
    while(fast && fast.next) {
      fast = fast.next.next;
      slow = slow.next;
      if(fast === slow) {
        return true;
      }
    }
    return false;
  };
Copy the code