How to judge the loop in a single chain list and its proving process

Source: Internet
Author: User

Problem:

1. How can I tell if there is a ring in the single-linked list?

The idea of the algorithm is to set two pointers p, Q, where P moves forward one step at a time, and Q moves forward two steps at a time. Then if the single-linked list exists, then p and Q meet; otherwise Q will first encounter null.
Here is the main understanding of a problem, is why when the single-linked list of rings, p and Q will definitely meet?


Assuming that the length of the single-linked list is n, and that the single-linked list is annular, the first iteration, p points to the element I mod n,q points to 2i mod N. So when i≡2i (mod n), p meets Q. and i≡2i (mod n) = (2i-i) mod n = 0 + i mod n = 0 = When i=n, p meets Q. A simple understanding here is that P and Q are running on the playground at the same time, where Q is twice times the speed of P, and when both of them start at the same time, p runs a lap to reach the starting point, and Q is just running two laps to reach the starting point.
So what happens when P and Q start different? Assume that the first iteration of P points to element I mod n,q points to k+2i mod n, where 0<k<n. So I≡ (2i+k) (mod n) = (i+k) mod n = 0 = when i=n-k, p and Q meet.

The key here is to understand, in fact, the fast pointer and the slow pointer are not in the ring at first, but when they all go into the ring, the problem model is actually equivalent to two pointers in the ring from the beginning of different models.

Understand this, and many subsequent proofs are well understood.

Extended reading:

Promote:

1. If the speed of the two pointers is different, such as P,q, (0<p<q) What kind of relationship can be satisfied, so that both of them will be bound to a node?

Sp (i) = Pi

Sq (i) = k + Qi

Sp (i) = Sq (i) = (pi) mod n = (k + qi) mod n =>[(q-p) i + K] mod n =0 if two are to intersect with one node

= = (Q-p) i + k = Nn [N is natural number]

= = i = (nn-k)/(P-Q)

I take the natural number, then when the p,q satisfies the above equation that there is a natural number n, can satisfy the nn-k is a multiple of p-q, ensure that the two intersect.

Special case: If Q is twice times the step of P, all starting from the same starting point, that is, q = 2p, k = 0, then the equation becomes: nn=i: It can be understood that when I iteration, I is the integer times of the circle, both can be cross, the intersection is the starting point.

2. How to determine the length of a single linked list ring?

This is relatively simple, know Q has entered into the ring, save the position. The position is then traversed, and when the Q position is hit again, the number of iterations is the length of the loop.

3. How do I find the first node in the ring in the list?

Assuming that the chain table length is L, the first half of the length is k-1, then the second ring in the node is k, the length of the ring is N, then when the q=2p, when the first intersection? When the Q pointer goes to the K-node, the Q pointer is already in the position of the ring's K mod n. That is, the p and Q difference k elements, starting from different starting point, then the intersection position is n-k, then the following figure:

As can be seen from the diagram, when p from the position of the intersection (N-K), the forward traversal of the K-node to reach the first point of the ring, node K.

The algorithm is simple: A pointer from the position of the first intersection of P and Q (N-K), and another pointer from the list head, the intersection of which is the first link in the list in the ring intersection.

How to judge the loop in a single chain list and its proving process

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.