Write a program to find the node at which the intersection of the singly linked lists begins.
For example, the following, linked lists:
A: a1→a2 c1→c2→c3 B: b1→b2→b3
Begin to intersect at node C1.
Notes:
- If The linked lists has no intersection at all, return
null
.
- The linked lists must retain their original structure after the function returns.
- You may assume there is no cycles anywhere in the entire linked structure.
- Your code should preferably run in O (n) time and use only O (1) memory.
Analysis: This problem is very interesting, Lenovo to the previous LinkedList cycle II, we can not difficult to think of reverse B and then the A and B together, if A and B have a intersection, then A and b must have a ring, at this time we will be able to use the linked list cycle The solution of II. The code is as follows:
1 classSolution {2 Public:3ListNode *getintersectionnode (ListNode *heada, ListNode *headb) {4 if(Heada = = NULL | | headb = = NULL)returnNULL;5 if(Heada = = headb)returnHeada;6 7ListNode *end = reverse (HEADB);//Reverse B8Headb->next = Heada;//Construct a circle9 TenListNode *result =detectcycle (end); OneHeadb->next =NULL; AHEADB =reverse (end); - - returnresult; the } -ListNode *reverse (ListNode *head) { - if(head = = NULL | | head->next = = NULL)returnhead; -ListNode Dummy (-1); +Dummy.next =head; - for(ListNode *pre = head, *p = head->next; p; p = pre->next) { +Pre->next = p->Next; AP->next =Dummy.next; atDummy.next =p; - } - returnDummy.next; - } - -ListNode *detectcycle (ListNode *head) { in if(head = = NULL | | head->next = = NULL)returnNULL; - toListNode *slow = head, *fast =head; + //Find Meeting point - while(Slow->next && Fast && fast->next) { theslow = slow->Next; *Fast = Fast->next->Next; $ if(slow = = fast) Break;Panax Notoginseng } - //No cycle the if(Slow! = fast)returnNULL; + //Find Cycle beginning AFast =head; the while(Fast! =slow) { +Fast = Fast->Next; -slow = slow->Next; $ } $ returnfast; - } -};
There is another solution to this problem, which uses geometric distance relations, we can quickly find the intersection point as long as the difference d of the distance between the two linked list and the intersection point. When we traverse the short-length linked list, the long linked list accesses only the end of the record at the distance of D. We can design the algorithm by this point. Assuming that two linked list A and B, a is less than the length of B, we use PA and PB to traverse two linked lists, when the PA reaches the end, the PA is updated to the head of the list B, and then Traverse, know PB reached the end, at this time PB updated to the head of the list A, continue to traverse until PA=PB. The code is as follows:
1 classSolution {2 Public:3ListNode *getintersectionnode (ListNode *heada, ListNode *headb) {4 if(Heada = = NULL | | headb = = NULL)returnNULL;5 if(Heada = = headb)returnHeada;6 7ListNode *pa = Heada, *PB =headb;8 while(PA &&PB) {9PA = pa->Next;TenPB = pb->Next; One } A if(PA = = NULL && PB = =NULL) { -PA =Heada; -PB =headb; the}Else if(PA = =NULL) { -PA =headb; - while(PB) { -PB = pb->Next; +PA = pa->Next; - } +PB =Heada; A}Else{ atPB =Heada; - while(PA) { -PA = pa->Next; -PB = pb->Next; - } -PA =headb; in } - while(PA &&PB) { to if(PA = PB)returnPA; +PA = pa->Next; -PB = pb->Next; the } * returnNULL; $ }Panax Notoginseng};
Leetcode:intersection of Linked Lists