Problem 1: (the beauty of referencing programming) If two linked lists overlap, the end node must be public
Question 2:
1 int listlength (node * List) {2 int length = 0; 3 while (list! = NULL) {4 length ++; 5 List = List-> next; 6} 7 return length; 8} 9 // 10 node * firstcomnode (node * list1, node * list2) {11 int n1 = listlength (list1); 12 INT n2 = listlength (list2); 13 // defines the pointer plong, Pshort, point to the long list and short list 14 node * plong = list1; 15 node * Pshort = list2; 16 if (N1 <N2) {17 plong = list2; 18 Pshort = list1; 19} 20 // move the pointer to the long list a certain distance to D21 int d = (n1-n2)> 0 )? (N1-n2) :( n2-n1); 22 while (d --) {23 plong = plong-> next; 24} 25 // two pointers traverse to the tail at the same time, find the first same node 26 while (plong! = NULL & plong! = Pshort) {27 plong = plong-> next; 28 Pshort = Pshort-> next; 29} 30 return plong; 31}
Determines whether two linked lists are intersecting. searches for the first public node of the two linked lists.