Determine if the two linked lists intersect, to find the intersection (assuming that the linked list does not have a ring)
Determine if the two linked lists intersect to find the intersection (assuming that the linked list may have loops)
650) this.width=650; "title=" qq20160120200455.jpg "src=" http://s3.51cto.com/wyfs02/M02/79/FF/ Wkiol1agdtqrcpnaaadaezwg7ay053.jpg "alt=" Wkiol1agdtqrcpnaaadaezwg7ay053.jpg "/>
Ringentry_point () and other functions see the previous chapter.
Slistnode* intersect (SLISTNODE *&L, SLISTNODE *&M)//To determine whether the two linked lists intersect, to find the intersection (assuming the linked list without a ring) { / /idea: If there is no ring, only intersect/do not want to cross two cases // the same as Ringentry_point () function method: // Two list length difference K, Let a pointer start with a long list of K-steps, and another pointer starting from the short-linked list, // two linked list to go together, the meeting point is the entry point if (l != null&&m != null) { SListNode *cur = L; Slistnode *newnode = m; int c1 = _lengthnode (cur); int c2 = _lengthnode (NewNode); int minus = _lengthnode (cur) - _lengthnode (NewNode); if (minus > 0) { while ( minus--) { cur = cur->next; } } else { int tmp = -minus; while (tmp--) { newnode = newnode->next; } } while (newnode != cur&&newnode != null&&cur != null) { NewNode = NewNode->next; cur = cur->next; } if (newnode == null | | cur == null) return null; return cur; } return null;} slistnode* intersectring (SLISTNODE *&L, SLISTNODE *&M)//Determine whether the two linked lists Intersect, Finding the intersection (assuming that the chain list may have a ring) { //thought: Consider the whole case: 1, without ring intersect/disjoint 2, with ring one band ring without ring/two band ring (where the intersection is not on the ring/intersection on the ring) //distinguish between the circumstances, respectively, to achieve the if (l != null&&m != null) { if (! IsRing (L) && ! IsRing (M))//Without ring return intersect (l, m); else if ((IsRing (L) && ! IsRing (M)) | | (! IsRing (L) && isring (M))//one strip without ring return NULL; else { //first to find the ring entry point of the two linked lists, if the entry point is the same, then the 5th case is 4th, 6 kinds of circumstances slistnode *re1 = ringentry (L); slistnode *re2 = ringentry (M); if ( Re1 == re2)//5th case The problem of cross-linked list without ring    {    SLISTNODE* M1 = isring (L); slistnode*recover = m1->next;// restoration modeled after Ringentry _point () function m1->next = null; slistnode*cur = intersect (l, m); m1->next = recover; return cur; } else//4th, 6 cases if one of the ring entry points is on another ring, it is the 6th case, otherwise the 4th case { slistnode *tmp = re1->next; while&nBSP; (tmp != re1&&tmp != re2) tmp = tmp-> next; if (Tmp == re1)//4th case return null; printf ("Two-chain bracelet, with two intersections \ n");//6th Case     SLISTNODE *TMP1 = re1->next; re1->next = NULL; Printnode (Re1); re1->next = tmp1; return re2; } } } else return null;}
Test
Void test10 () { printf ("//test10 () intersect () \n"); slistnode *ll = Null; pushback (ll, 1); pushback (ll, 2); pushback (ll, 3); PushBack (LL, 4); pushback (ll, 5); printnode (LL); slistnode *mm = null; pushback (MM, 1); pushback (mm, 2); mm->next->next = ll->next->next->next; printnode (MM); slistnode *nn = null; pushback (nn, 7); PushBack (NN,  8); pushback (nn, 9); pushback (nn, 0); printnode (NN); slistnode* c1 = intersect (LL, MM); slistnode* c2 = intersect (LL, NN); Printnode (C1); printnode (C2);} Void test11 () { printf ("//test11 () intersectring () \n"); slistnode *ll = null; pushback (ll, 1); pushback (ll, 2); pushback (ll, 3); PushBack (LL, 4); pushback (ll, 5); printnode (LL); slistnode *nn = null; Pushback (nn, 6); pushback (nn, 7); pushback (nn, 8); pushback (NN, 9); pushback (nn, 0); printnode (NN); slistnode* c1 = intersectring (LL, NN) ;//1th printnode (C1), printf ("\ n"), slistnode *mm = null;//2nd pushback (MM, 0); pushback (mm, 1); mm->next->next = ll->next; slistnode* C2 = intersectring (LL, MM); printnode (C2); printf ("\ n"); find (LL, 5) Next = find (ll, 3);//3rd Type slistnode* c3 = intersectring (LL, NN); Slistnode* c4 = intersectring (NN, LL); printnode (C3); printnode (C4); printf ("\ n"); find (nn, 0)->next = find (nn, 8);//4th SListNode* c5 = Intersectring (nn, LL); printnode (C5); printf ("\ n"); slistnode* c6 = intersectring (MM, LL);// 5th species of slistnode* c7 = intersectring (ll, mm); slistnode* tmp1 = c6- >next; c6->next = null; printnode (C6); c6->next = tmp1; Slistnode* tmp2 = c7->next; c7->next = null; printnode (C7); c7- >next = tmp2; printf ("\ n"); find (nn, 0)->next = find (LL, 4);// 6th species of slistnode* c9 = intersectring (NN, LL); slistnode* tmp3 = c9- >next; c9->next = null; printnode (C9); c9->next = tmp3; Slistnode* c10 = intersectring (LL, NN); slistnode* tmp4 = c10->next ; c10->next = null; printnode (C10); c10->next = tmp4; printf ("\ n "); find (ll, 5)->next = find (ll, 1);//6th find (nn, 0)->next = find (LL, 4); slistnode* c11 = intersectring (NN, LL); slistnode* tmp5 = c11- >next; c11->next = null; printnode (C11); c11->next = tmp5; Slistnode* c12 = intersectring (LL, NN); slistnode* tmp6 = c12->next ; c12->next = null; printnode (C12); c12->next = tmp6; printf ("\ n ");}
650) this.width=650; "title=" qq20160121140311.jpg "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M01/7A/00/ Wkiom1agd4jzcvu7aaccidenpxm318.jpg "alt=" Wkiom1agd4jzcvu7aaccidenpxm318.jpg "/>
650) this.width=650; "title=" qq20160121140516.jpg "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M01/7A/00/ Wkiom1agd4rwhvvbaael-phfnle276.jpg "alt=" Wkiom1agd4rwhvvbaael-phfnle276.jpg "/>
< written >< interview >c/c++ single-linked list (4) determine if the two linked lists intersect, find the intersection (linked list without ring/May band ring)