Method for Determining whether a linked list has a ring or a ring entry point (linked list cycle II)

Source: Internet
Author: User

Two steps

The first step is to use the speed pointer. If there is a ring, the speed pointer will eventually meet on a node.
Step 2. Then, start from this node and take a step at the same time from the root node. The place where the two pointers meet is the entrance of the ring.

The first step explains why the second step is?
There are many solutions on the Internet that are mostly analyzed from the mathematical point of view, and there are formulas and calculations that are not intuitive. From the graphic point of view, it is much easier to understand.



Spread the graph into a line. If we have a ring and assume that the fast pointer goes through one more lap, it will be the same as the slow pointer (n more laps are actually the same. It is not hard to understand the plot, it is represented by a circle when it is difficult to draw)


The red line indicates that the fast pointer has gone through the 2 m blue line, indicating that the slow pointer has gone through M, which can be intuitively seen. If you move the Blue Line to the red line above, the red line will be blocked, the number of remaining red lines

It is a Y and a Z. We know that the length of the multiple steps is m, that is, Y + z = m. The blue line is also M, that is, x + z = m.


So if we launch x = y directly, we will understand the second step.


If the fast node goes through a lot of circles, we can draw our own pictures and find that the model of the above figure is actually down.

Code Attached

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode detectCycle(ListNode head) {        if(head==null){            return null;        }        ListNode slow = head;        ListNode fast = head;        ListNode now = head;        while(fast.next!=null&&fast.next.next!=null){            fast = fast.next.next;            slow = slow.next;            if(fast==slow){                now = slow;                break;            }        }        if(fast.next==null||fast.next.next==null)            return null;        else{            ListNode  start = head;            while(start!=now){                start = start.next;                now = now.next;            }            return start;        }    }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.