The implementation of the algorithm to review 2--chain list ring

Source: Internet
Author: User

The most classic link list is the fast and slow pointer. And today's protagonist is it.

The idea of a fast and fast pointer is that at the same time the start of the tortoise and rabbits, if the track without ring, will never meet, on the contrary would meet.

Then we assume that rabbits are twice times faster than turtles.

The tortoise and the rabbit meet at some point in the ring, having the following equation: 2*x=x+n*k where x is the distance the tortoise has traveled, N is the number of laps the rabbit is circling, and K is the circle length.

The equation transforms the x=n*k, understanding is that the turtle walks just enough to go around the N circle ring, and this distance is from the starting point to the current position of the distance.

So, let the other turtle start from the starting point at the moment, and 2 turtles move on.

According to the equation, we will meet again at the original point. Think again, the two are the same speed, then the two have been synchronized forward after the first encounter.

The meeting point is bound to be the beginning of the ring, otherwise the same speed and direction of the same, it is inevitable to meet.

The implementation of the algorithm to review 2--chain list 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: 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.