Determine if a single-linked list has ring and ring connection points

Source: Internet
Author: User

Given a single linked list, give only the pointer h:

1, how to determine whether there is a ring?

2, how to know the length of the ring?

3, how to find out where the ring connection point?

4. What is the length of the linked list?

Solution:

1, for the problem 1, using a catch-up method, set two pointers slow, fast, starting from the beginning of the hands, each step forward 1 steps, 2 steps. If there is a ring, the two meet; fast encounters a null exit if no ring exists.

2, for the problem 2, recorded the problem 1 of the collision point P,slow, fast from the point of the start, the number of collisions traversed by the length of the ring S.

3. Question 3: There is a theorem: the distance from the collision point P to the connection point = the distance from the head pointer to the connection point, so, starting from the collision point and the head pointer, the point of encounter is the connection point. (Proof in the back note)

4, the problem 3 has been found in the length of the connection point distance head pointer, plus the length of the ring found in Problem 2, the sum of which is the length of a single linked list with a ring

voidIsloop (Llink head) {if(!head| |! Head->next)return; Llink p,q; BOOLloop=false; p=q=head->Next; while(Q&&q->next)//determine if there is a ring{p=p->Next; Q=q->next->Next; if(p==q) {loop=true;  Break; } } if(!loop) cout<<"This link is not loop\n"; Else{cout<<"This link has a loop\n"; Llink R=p; Q=head->Next; intnonloop=1, loopcount=1; //Nonloop compute non-ring node points, Loopcount compute the node nodes on the ring   Do//calculate the number of nodes on a ring{p=p->Next; ++Loopcount; } while(p!=R); --Loopcount;  while(P!=Q)//Get the entry node of the ring and calculate the number of non-ring nodes{p=p->Next; Q=q->Next; ++Nonloop; }  --Nonloop; cout<<"\nstart of loops:"<<p->data<<Endl; cout<<"\ncount of Nonloop:"<<Nonloop<<"\ncount of loops:"<<Loopcount<<"\ncount of Linknode:"<<nonloop+loopcount<<Endl;}}

Procedure to determine if a loop exists:

BOOL Isexitsloop (Slist *head)  {      *slow = head, *fast = head;        while (Fast && fast->next)       {          = slow->next;           = Fast->next->Next;           if  Break ;      }         return ! (fast = = NULL | | fast->next = = null);  }  

A program that looks for a ring connection point (entry point):

slist* Findloopport (Slist *head) {Slist*slow = head, *fast =Head;  while(Fast && fast->next) {Slow= slow->Next; Fast= fast->next->Next; if(slow = = fast) Break; }        if(Fast = = NULL | | fast->next = =NULL)returnNULL; Slow=Head;  while(Slow! =fast) {Slow= slow->Next; Fast= fast->Next; }      returnslow; } 

You can also use a method similar to the hash table, that is, to set up an array, the values in the list node array subscript, when the assignment conflict is the ring access point

  

 BOOLIsloop (Llink p) {if(!p| |! P->next)return true; inta[maxsize],n=0; memset (A,0,sizeof(int)*MAXSIZE); P=p->Next; while(p) {if(a[p->data]==-1)//conflicts occur when there is a ring{cout<<"\nloop node:"<<p->data<<Endl<<"\nlen of node:"<<n<<Endl; return true; } a[p->data]=-1; ++N; P=p->Next;} return false;} Llink Creatlinkloop ()

 BOOLIsloop (Llink p) {if(!p| |! P->next)return true; inta[maxsize],n=0; memset (A,0,sizeof(int)*MAXSIZE); P=p->Next; while(p) {if(a[p->data]==-1)//conflicts occur when there is a ring{cout<<"\nloop node:"<<p->data<<Endl<<"\nlen of node:"<<n<<Endl; return true; } a[p->data]=-1; ++N; P=p->Next;} return false;} Llink Creatlinkloop ()

////////////////////////////////////////////////////////

Note

The proof of question 2 is as follows:

The linked list shape resembles the number 6.
Assume that the tail (outside the ring) length is a (number of nodes), the length of the ring is B.
The total length (also the sum of points) is a+b.
Starting from scratch, the 0 base number.
The node visited by step I is represented by S (i). i = 0, 1 ...
When i When I≥a, S (i) =a+ (i-a)%b.

Analyze the catch-up process:
Two pointers go forward respectively, assuming that after X-steps, the collision. Then there are: S (x) =s (2x)
The periodicity by the ring is: 2x=tb+x. Get X=TB.
Another, when the collision, must be in the ring, it is impossible in the tail section, there is x>=a.

The connection point is a step from the starting point, i.e. S (a).
S (a) = S (tb+a) = S (x+a).
The conclusion is that a step forward from the collision point X is the connection point.

According to the hypothesis, S (A-1) is in the tail section, S (a) on the ring, while S (x+a) is bound on the ring. So collisions can occur.
And, the same as the forward step A, the same as the connection point, so there must be a collision.

In conclusion, the first collision point is the connection point, from the point of X and from the starting point.

/////////////////////////////////////////////////////////////

Assuming that the total length of the single-linked list is L, the distance from the head node to the ring inlet is a, the ring inlet to the fast and slow pointer meet the node distance is x, the ring length is r, the slow pointer took a total of s step, then the quick pointer walked 2s step. In addition, the fast pointer to catch up with the slow pointer, the fast pointer to at least in the ring in a circle of more than a lap (assuming the n-circle plus x distance), get the following relationship:
s = a + x;
2s = a + nr + x;
=>a + x = nr;
=>a = Nr-x;
From the above: if the head node and the encounter node set a pointer, synchronous (single Step) forward, then the final must meet in the ring entrance junction, Fix!
Figures:

Determine if a single-linked list has ring and ring connection points

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.