The programming of the Microsoft Institute----to determine whether two linked lists Intersect
Give the head pointers of two unidirectional lists, such as H1,H2, to determine if the two linked lists intersect.
Analysis:
(1) First determine whether the chain list has no ring, if one has a ring, one without a ring, then two linked lists will certainly not intersect.
(2) If the two lists have no ring, then if the two linked lists intersect, the last node of the two linked list must be the same node.
(3) If the two linked list h1,h2 have a ring, you can find two linked lists and any node on the ring P1 and P2. If you start traversing the linked list H1 from the node P1, you can traverse to P2, which means that the two linked lists intersect, otherwise the H1 is traversed from the P1, the loop goes back to P1, and the P2 is not traversed, indicating that the two linked lists do not intersect.
(4) Check whether the single-linked list has a ring method:
Using two pointers p1,p2 from the linked list head, p1 each step forward, P2 each advance two steps. If P2 arrives at the end of the list, indicating no ring, otherwise P1, P2 will inevitably meet at some point (P1==P2), thus detecting the link list in the ring.
#include <stdio.h>#include<stdlib.h>typedefintElemtype;//defining the storage structure of a linked listtypedefstructNode {elemtype data; structNode *Next;} Node;typedefstructNode *Linklist;typedefstructNode *Pnode;//determine if two linked lists intersectintislistjoined (pnode p1, pnode p2);//determine if the single-linked list has no ring, and if there is a ring, return any node on the ring, otherwise return null .pnode testcycle (linklist L);//Suppose two single-linked lists have no loopsintisjoinedsimple (Pnode H1, Pnode H2);//Create a single-linked list (n indicates the chain table length, iscycle Indicates whether there is a ring)Linklist Createlinklist (intNintiscycle);//determine if two linked lists intersectintislistjoined (pnode p1, Pnode p2) {Pnode cycle1=testcycle (p1); Pnode Cycle2=testcycle (p2); if((Cycle1! = null && Cycle2 = = null) | | (Cycle1 = = NULL && Cycle2! =NULL)) { //if one has a ring, one without ring return 0; } if(Cycle1 = = NULL && Cycle2 = =NULL) { //None of the two have rings . returnIsjoinedsimple (P1, p2); } //all two have rings .Pnode p =Cycle1; while(1) { if(p = = Cycle2 | | p->next = =cycle2) { return 1; } P= p->next->Next; Cycle1= cycle1->Next; if(p = =cycle1) { return 0; } }}//determine if the single-linked list has no ring, and if there is a ring, return any node on the ring, otherwise return null .pnode testcycle (linklist L) {pnode P1=L; Pnode P2=L; while(P2! = NULL && P2->next! =NULL) {P1= p1->Next; P2= p2->next->Next; if(P1 = =p2) { returnP1; } } returnNULL;}//Suppose two single-linked lists have no loopsintisjoinedsimple (Pnode H1, Pnode h2) { while(h1->next) {H1= h1->Next; } while(h2->next) {H2= h2->Next; } if(H1 = =H2) { return 1; } return 0;}//Create a single linked listLinklist Createlinklist (intNintiscycle) {linklist L=NULL; Pnode p, R=NULL; Elemtype e; for(inti =0; I < n; i++) {printf ("Please enter the number of%d digits", i +1); scanf ("%d", &e); P= (Pnode) malloc (sizeof(Node)); if(p = =NULL) {printf ("Out of space"); Exit (1); } P->data =e; if(i = = n1&& Iscycle = =1) {p->next = L;//this is where the next pointer to the tail node of the list points to the head node .}Else{p->next =NULL; } if(L = =NULL) {L=p; } Else{R->next =p; } R=p; } returnL;}intMainintargcConst Char*argv[]) { //Create two linked lists without loops, and do not intersectlinklist H1 = Createlinklist (Ten,0); linklist H2= Createlinklist (5,0); intIsJoined1 =islistjoined (H1, H2); printf ("%d", isJoined1); //Create two linked lists without loops, and intersectlinklist h3 = Createlinklist (5,0); Linklist h4=H3; intIsJoined2 =islistjoined (H3, H4); printf ("%d", ISJOINED2); //Create two linked lists with loops and do not intersectlinklist h5 = createlinklist (5,1); linklist h6= Createlinklist (4,1); intISJOINED3 =islistjoined (h5, H6); printf ("%d", ISJOINED3); //Create two linked lists with loops and intersectlinklist H7 = Createlinklist (5,1); Linklist H8=H7; intIsJoined4 =islistjoined (H7, H8); printf ("%d", ISJOINED4); return 0;}
Determine if two linked lists Intersect