Single link table with ring for midpoint

Source: Internet
Author: User

Recently has been no brain knocking code, the results of this afternoon by a student to prepare a variety of interview questions asked a single linked with the ring list of problems, found that the recent IQ is not enough to use, think for a little longer, and finally think to understand. Because the last thought of the big time, Baidu a bit, so have to choose the classification for reprint, the following on the basis of their own thinking, to explain, such as the wrong place, hope to correct.


First of all, for an n-node single chain ring, if given a start node n0, and then with a,b two pointers, respectively, 1 and 2 of the step traversal, then in a ring of how many laps (M), a distance n0 have an offset of the case, the a,b will coincide. Point to the same node.

>>> Let's skip the mathematical derivation of the trouble to prove anything, first give the answer. After a walk a lap, A,B will return to the N0 node, also that a,b coincide, so m=1,offset = 0.

Why.

>>> Some people may take it for granted that the offset and the number of laps may be random (well, at least my classmate thought so at first). Don't be distracted by the 1 and 2 steps above, in fact, for integers n (>=1) and 2N, it is obvious that the LCM of these two numbers is 2N, it is impossible to 2n+x, a rough description of the offset is 0, and GCD is n itself, a rough description of the number of laps is 1 ... A simple way to say: In a circle, A,b will not coincide, if the number of nodes n is even, then B just reached a lap, a happened in the middle (the first half of the last one o'clock). Although the increment of B is big, but a is ahead of B. A next to the N/2 node, and B is facing n nodes, so after moving N/2 times, A and B coincide in the starting point.


Secondly, based on the above conclusion, for an n-node single chain ring, when A and b are traversed by 1 and 2 step lengths from different nodes on the ring, A and B will overlap.

>>> at first I and my classmates took it for granted that it would coincide, because considering this is a face test, not so weird, so the feeling will coincide. But if you want to prove it. Take advantage of the above conclusions. One might suspect that the above superposition of the reverse process cannot be exhaustive all the possibilities here. In fact, this difference is too worrying. You can consider this. From the starting point, each move, because the increment of B is 2,a is 1, so the distance between A and B will be increased by 1 with the number of moves. And the overall process, B move 2N Point, a move N points, so can produce [0, N) (half open half closed interval) of the initial distance. To sum up, will coincide.


Next, for a single linked list of L (unknown) nodes, the number of nodes that belong to the ring part is L0 (unknown), and the non loop part is L1 (unknown), so how do you know the entry node of the loop?

>>> first of all, in fact, the L0 here is knowable. Based on the first two, when you start at the head of the chain, using a and b two pointers to traverse the list at 1 and 2, the first coincidence of A and B can inspire such an operation, that is, let A and B continue to traverse and count, in a next coincidence when you can know that the ring is long, that is L0.

>>> then, here to a link http://www.douban.com/note/172176904/, before this I have been a god yy state, not how to use the formula, so here when the card, now skillfully borrow the wisdom of predecessors, continue to explain. PS: The contents of the link inside, I also a little doubt that the author has no thorough thinking, but looked at the other people's things only to be enlightened but the fact.

Set: A passes the number of nodes for La = L1 + offset,lb = L1 + offset + n*l0 =2 (L1 + offset). (This is different from the content in the link.) If you accept what I said earlier that a only needs to traverse a circle to coincide with B, then there is no need to say more.

Then: L1 + offset = N * L0.

Based on the above, on the basis of the L0, and then use the link said, after the coincidence, b again from the node to the 1 step traversal (this becomes 1), then in the next overlap, is the ring of the entry node. Because the step size is 1, the last offset is traversed at the same time.

At this point, the problem is over.

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.