C linked list of fast and slow pointer lookup loop node

Source: Internet
Author: User

Reference: http://blog.csdn.net/wenqian1991/article/details/17452715

The above analysis is based on this picture

Pull out the math formula. Just contact can't get it right. The following analysis is combined with the above article. Take a closer look,

General set fast pointer speed is twice times the slow pointer. And the fast pointer iterates through two pointers at a time, and the slow pointer iterates through 1 pointers at a time.

Suppose that the fast pointer meets the e point, the distance between the point of meeting and the Loop node D is x. Head Node A is k away from the loop node D.

Then in the two hands meet, each walk distance (here can think of it as a playground, starting point is not in the playground):

Slow pointer:

K + X + N (x+y) = M;//x+y The distance around the ring; n the slow pointer is a total of a few laps around the ring.

Quick pointer:

Imagine that the fast pointer is twice times the speed of the slow pointer, and the time it takes to meet them is the same. So the quick pointer goes through the distance

2*m;

Also equals

K+x +n* (x+y) = 2*m;//n The number of laps to bypass for fast pointers

Do not make the above two formula.

(n-n) * (x+y) = m; And

(n-n) * (x+y) = k+x+n* (x+y);//Here the X+y ring length is a fixed value. Assuming that the ring length is M

There are: (n-n) *m = k+x+n*m;

There are: K+x = (n-2*n) *m;

The final demolition formula comes out. The distance from the head node A to the Loop node D plus the meeting point e is the integer multiple of the ring length from the loop node D.

This formula is tested on the 0-type circular link list and the 6-type circular link list.

For the former K and X are 0; fast pointer starting point is a loop node (type 0 Any point is a loop node)
Then there is (n-2*n) *m = 0;

and N = 2*n; The number of rings around a fast-speed hand is twice times that of the latter when met. You can imagine the speed is 2 Bai, the same time.

It doesn't matter how many nodes there are in the ring.

Above just found the meeting node. How to find the loop node. For type 6 circular linked list.

Or the above-pushed formula:

K+x = (n-2*n) *m;//assumes n-2*n = Q; The unit is the number of laps.

Have k+x=q*m; Then assume that the fast pointer can be recycled node meet, then x = 0;

K = q*m; The value of Q is proportional to K,

Suppose you start with a quick pointer from the head node. The slow pointer starts at the point where the last pointer meets. Both have moved at the same speed.

When the D-cycle node of the fast pointer walks through the distance is k, the slow pointer goes to the D-cycle node to walk the distance of q*m;

At this point the two encounters node is the loop node.

Analyze the following code:

node* findbeginning (Node *phead) {      if(NULL = =phead)returnNULL; Node*fast =Phead; Node*slow =Phead; /*determine if there is a ring*/       while(Fast->pnext! = NULL)//two cases will jump out of the loop{Fast= fast->pnext->Pnext; Slow= slow->Pnext; if(NULL = =fast)returnNULL; if(Fast = =slow) Break; }        if(NULL = = Fast->pnext)//determine which situation causes the loop to jump out        returnNULL; /*Find ring Start*/Fast=Phead;  while(Fast! =slow) {Fast= fast->Pnext; Slow= slow->Pnext; }        returnFast; }  

C linked list speed pointer lookup loop node

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.