Analysis:
If the two linked lists are intersecting, the two linked lists are Y-shaped, and the last node must be the same. If the intersection is located, the long linked list is used to subtract the short one from the short one, the two linked lists move one node at a time until the intersection is the intersection.
Code:
NODE* FindNode(NODE* pHead1, NODE* pHead2){ NODE* p1 = pHead1; NODE* p2 = pHead2; int i = 1, j = 1, k = 0, f = 0; if(pHead2 == NULL || pHead2 == NULL) { return NULL; } while(p1->next != NULL) { p1 = p1->next; i++; } while(p2->next != NULL) { p2 = p2->next; j++; } if(p1 != p2) { return NULL; } else { p1 = pHead1; p2 = pHead2; f = fabs(i, j); if(i > j) { for(k=0; k<f; k++) { p1 = p1->next; } while(p1 != p2) { p1 = p1->next; p2 = p2->next; } return p1; } else { for(k=0; k<f; k++) { p2 = p2->next; } while(p1 != p2) { p1 = p1->next; p2 = p2->next; } return p1; } }}
Determine whether two one-way linked lists have intersection and find the intersection.