Determine if the chain list has a ring, the inlet of the ring, and the length of the ring

Source: Internet
Author: User

1, the judgment has the ring

A linked list if there is no ring, then always next, will eventually get null. If there is a ring, it must not be null. With this idea, you can save the linked list element for each traversal to a list, each time judging whether it is contained in the linked list, or whether it is empty. This is undoubtedly a viable option, but the controls that need to be consumed will become larger. There is also a classic way to solve this problem, that is the speed pointer.

Define two pointers, one called fast, and one called Slow. Through the two pointers through the chain list, as the name suggests, fast go faster, slow go slow, if there is a ring, they will meet, is you go after someone, as long as he will be able to catch up faster. Of course, the end condition is still null. Let's imagine that if you run at the same position in a ring with two pointers running at the same time, you will definitely meet at the end of the second lap, that is, the starting position. So the above problem, as long as the meeting, the slow pointer must not be more than a lap, the fast pointer is not necessarily, may be n-circle.

2, judge the entrance of the ring

Look at a diagram first


Suppose that in a linked list with a ring, two pointers go together and meet at 243 o ' clock. Set a starting point to the entry point distance of a, the entry point to the meeting point is X, the length of a ring is C.

Look at the following formula:

The fast pointer goes a+x+nc, the slow pointer goes a + x, and there is a condition, A+X+NC = 2 (A + x), which can be exported: a = (n-1) C + (c-x). The length of the position from the starting point to the entry point is equal to the n-1 circle plus the distance from the meeting point to the entry point. Now, if I let two equal-speed pointers start from the starting point, one from the point of encounter, and when the pointer from the starting point goes to the entry point, the pointer from the point of encounter N-1 and C-x, which is exactly the entry point, so we can get the entry point.

3, the length of the ring to seek

Knowing the entry point, it is too easy to find the length, just go through it again.


Determine if the chain list has a ring, the inlet of the ring, and the length of the ring

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: 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.