Original link: leetcode-cn.com/problems/li…

Answer:

  1. Use the fast or slow pointer to find if the linked list has a ring.
  2. If there are rings, going forward from the start of the list and the meeting point of the fast and slow Pointers will inevitably meet at the join point of the rings.
  3. The proof of the solution can refer to the second point of the official solution.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; DetectCycle = function (head) {{detectCycle = function (head) {{detectCycle = function (head) {{detectCycle = function (head) { Null if (! head || ! head.next) { return null; } // let slow = head; let fast = head; // If the fast pointer cannot move, exit the loop while (fast && fast.next) {// Slow = slow. Next; fast = fast.next.next; // If the two Pointers point to the same point, the ring has been found. If (slow === fast) {break; // If (slow === fast) {break; Return null if (slow!) {return null if (slow! == fast) { return null; } // If there are loops, and fast Pointers are twice as fast as slow Pointers. // So if you create two Pointers, start from the start of the list and the node where the fast and slow Pointers meet, respectively. // The node where the two meet must be the join point of the ring. let startNode = head; let meetNode = fast; // If two Pointers are equal, the join point is found. while (startNode ! == meetNode) { startNode = startNode.next; meetNode = meetNode.next; } return meetNode; };Copy the code