Without regard to the case of single linked list with ring
If 2 single-linked lists Intersect, it must be a Y-linked list
1. Traverse the 2 linked list to the tail node, record the length of the 2 linked list x,y2. The tail node is the same, then intersect. 3. Starting from the table header, the long list goes first |x-y|, followed by 2 linked lists, to determine the first of the same points.
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4typedefstructStudent//defining a linked list structure5 { 6 intnum; 7 structStudent *Pnext; 8}stu,*Pstu; 9 voidLink_tail_insert (Pstu, Pstu,int);//establishment of 2 linked lists by tail interpolationTen voidLink_show (PSTU);//Show linked list One voidLink_judge_intersect (Pstu, Pstu);//determine if the linked list has an intersection point A voidLink_bulid_intersect (Pstu, Pstu, Pstu, Pstu *);//set up a Y-linked list - voidMain () { - Pstu Phead1,ptail1,phead2,ptail2; the inti; -Phead1 =NULL; -Ptail1 =NULL; -Phead2 =NULL; +PTAIL2 =NULL; - while(SCANF ("%d", &i)! =EOF) { +Link_tail_insert (&phead1,&ptail1,i); ALink_tail_insert (&phead2,&ptail2,i); at } - link_show (PHEAD1); -Link_bulid_intersect (phead1,phead2,ptail1,&ptail2); - link_show (PHEAD2); - Link_judge_intersect (PHEAD1,PHEAD2); -System"Pause"); in - } to voidLink_tail_insert (Pstu *phead,pstu *ptail,inti) {//The tail interpolation method to establish the linked list + Pstu pnew; -Pnew = (pstu)malloc(sizeof(Stu)); thememset (Pnew,0,sizeof(Stu)); *Pnew->num =i; $ if(*ptail = =NULL) { Panax Notoginseng*phead =pnew; -*ptail =pnew; the } + Else{ A(*ptail)->pnext =pnew; the*ptail =pnew; + } - } $ voidLink_show (Pstu phead) {//Output Link List $ Pstu pshow; -Pshow =Phead; - if(Phead = =NULL) the { -printf"No exsit\n"); Wuyi return; the } - while(Pshow! =NULL) { Wuprintf"%d",pshow->num); -Pshow = pshow->Pnext; About } $Putchar ('\ n'); - } - voidLink_bulid_intersect (pstu phead1,pstu phead2,pstu ptail1,pstu *ptail2) {//The 2nd linked list is stitched to the position of the third node of the first list. - Pstu i; Ai =Phead1; +i = i->pnext->Pnext; the(*PTAIL2)->pnext =i; -*PTAIL2 =Ptail1; $ } the voidLink_judge_intersect (Pstu phead1,pstu phead2) {//determine if the linked list has an intersection point the intx, y; the Pstu I,j,prei,prej; thex = y =0; -i = Prei = Phead1;//establish the front node, and the current node . inj = Prej =phead2; the while(I! = NULL) {//2 linked lists run to tail thePrei =i; Abouti = i->Pnext; theX + +; the } the while(J! =NULL) { +Prej =J; -j = j->Pnext; they++; Bayi } the if(Prei! = Prej)//The tail node is not the same, no intersect. theprintf"Link has no intersect.\n"); - Else{ -i =Phead1; thej =phead2; the if(x > Y) {//1th Link list length long, first X-y step thex = x-y; the while(X >0){ -i = i->Pnext; thex--; the } the while(I! = NULL) {//to walk together, to know the encounter or to reach null94 if(i = =j) { theprintf"Link has intersect.\n"); theprintf"%d\n",i->num); the Break; 98 } Abouti = i->Pnext; -j = j->Pnext; 101 } 102 } 103 Else{ 104y = y-x;//2nd link List length long, first X-y step the while(Y >0){ 106j = j->Pnext; 107y--; 108 } 109 while(I! = NULL) {//to walk together, to know the encounter or to reach null the if(i = =j) { 111printf"Link has intersect.\n"); theprintf"%d\n",i->num); 113 Break; the } thei = i->Pnext; thej = j->Pnext; 117 } 118 } 119 - } 121}
Determine if 2 single-linked lists Intersect and find the first intersection node