Determine whether there is a ring in a single-chain table

Source: Internet
Author: User

This question is slow and interesting.

Question: 0. How can I determine whether a single-chain table contains loops?

AlgorithmThe idea is to set two pointers p and q, where p moves one step forward and Q moves two steps forward each time. If a single-linked table has a ring, p and q will encounter each other; otherwise, Q will first encounter null.
Here, I mainly understand the question: why Will p and q meet each other when a single-chain table has a ring?

If the length of a single-chain table is n and the single-chain table is ring, P points to the I mod N element and Q points to 2I mod N in the I iteration. Therefore, when I have 2I (mod
N), p and q. While I need 2I (mod n) => (2I-I) mod n = 0 => I mod n = 0 =>
When I = n, p and q meet each other. A simple understanding here is that p and q are running on the playground at the same time, where Q is twice the speed of P. when both of them start at the same time, P is running at the starting point, Q is just now
After two laps, we can reach the starting point.
What if p and q start point are different? Assume that P points to the element I mod N in the I iteration, and Q points to K + 2I mod n, where 0 <k <n. Then I found (2I + k) (mod n) => (I + k) mod n = 0 => when I = n-k, p and q met.

Solution:

Promotion:

1. If the two pointers have different speeds, such as P, Q, and (0 <p <q), what is the relationship between them?

SP (I) = pi

Sq (I) = K + QI

If two nodes are to be intersecting, SP (I) = SQ (I) => (PI) mod n = (k + QI) MOD n => [(Q-p) I + k] mod n = 0

=> (Q-p) I + k = nn [N is a natural number]

=> I = (Nn-k)/(p-q)

When I take the natural number, when p and q meet the above equation, that is, there is a natural number N, which can satisfy the fact that Nn-K is a multiple of p-Q and ensure that the two are at the same intersection.

Special case: If Q is twice the step of P, it starts from the same start point, that is, q = 2 P, K = 0, then the equation becomes: NN = I: it can be understood that when I is an integer multiple of the circle in the I iteration, both of them can be handed over, and the intersection is the starting point.

2. How to determine the length of the ring in a single-chain table?

This is relatively simple, know that Q has entered the ring, and save the location. Then, the position is traversed. When the Q position is reached again, the number of iterations is the length of the ring.

3. How to find the first node in the ring in the linked list?

Assume that the chain table length is l, the first half of the length is K-1, then the node in the first loop is K, the length of the ring is N, then when Q = 2 P, when is the first intersection? When the Q pointer reaches the K node, the Q pointer is already in the K mod n position of the ring. That is, p and q differ by k elements. Starting from different starting points, the intersection position is n-k, and the following figure is displayed:

From the figure, we can see that when P traverses K nodes forward from the intersection point (n-k) to the first point of the ring, the node K.

The algorithm is simple: a pointer starts from the first intersection of p and q (n-k), and another pointer traverses from the linked list header, the intersection is the first intersection in the ring in the linked list.

4. If two single-chain tables have transactions? Where is the first intersection?

This problem can be easily converted into the previous question by drawing a diagram.

Associate the tail node in one of the linked lists with the header node, so we can easily find the problem and convert it to problem 3. We need to find the first node in the ring of the linked list with a 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.