How to judge the chain list has a ring, how to judge the link ring entrance

Source: Internet
Author: User

From http://blog.csdn.net/wuzhekai1985/article/details/6725263

----------------------

Question 1, how can I tell if a ring exists in a linked list? That is, the ring from E to r?

Set Slow/fast two pointers, starting at the beginning of the list, where fast pointer fast speed moves each length of 2,slow one at a time. If there is no ring, fast cannot coincide with the slow after the start of the traversal, and fast or fast->next eventually inevitably reach null;

If there is a ring, then fast must arrive at the ring no later than slow, and since fast moves with a step of 2,slow of 1, fast must be able to coincide with low (and necessarily the first coincidence) before the low enter loop continues around a week.

(! The above bold part is right, because slow is slower than fast , if there is a ring, before slow can traverse a linked list, fast will definitely traverse the side of the linked list, because fast ah, which fast and slow will certainly be met by the moment . )

The code can be as follows:

    BOOLHascircle (node* head, node* &encounter) {Node*fast = head, *slow =Head;  while(Fast && fast->next) {Fast= fast->next->Next; Slow= slow->Next; if(Fast = =slow) {Encounter=Fast; return true; }} Encounter=NULL; return false; }  

2, if there is a ring, then how can we find the entry point of the ring (ie above the e-point)?

Answer: As painted:

The distance between the start of the chain and the ring inlet is X,

Ring entry point to Problem 1 the distance between fast and slow coincident points is Y,

Fast has been around N weeks (n>0, certainly greater than 1) when fast and slow coincide.

And the total length of the slow move is s, the total length of the fast move is 2s,

The length of the ring is R.

So this time = = =

S+nr = 2s,n>0, <1> This step can be s-y+ny+y = 2s, s-y is the number of full laps for fast in the ring, and Y is the number of steps that fast has in the loop,

s = x+y <2>

by <1><2>, S = nr brought in <2>

NR = X+y

x = nr-y <3>

When fast meets slow, fast starts traversing the list header, and slow then traverses, both fast and slow are 1 steps. When fast and slow meet again, the meeting point is the entrance of the ring.

This conclusion is obtained from the formula <3>, because x is the length of the ring long r-y, when fast moved x length, slow will certainly also go along the ring r-y step.

The code is as follows:

    node* Findentry (node* head, node* Encounter)      {           *p1 = head, *P2 = encounter;            while (P1! = p2)          {              = p1->next;               = P2->next;          }           return p1;      }  

==============

How to judge the chain list has a ring, how to judge the link ring entrance

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.