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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
Hide TagsLinked List Two single list, to determine if there is an intersection, such as clear, the most intuitive method is for list1 begin-to-last for List2 begin to last if List2==list1 success endend time is O (nm) with very little space O (1). How to improve it?
- Traverse the list1 to have its nodes present hash_table
- Traverse List2, if already in hash_table, then exist
Using hash_table can increase the time to O (n+m), but the space becomes O (n).
1 classSolution {2 Public:3ListNode *getintersectionnode (ListNode *heada, ListNode *headb) {4unordered_map<listnode*,int>m;5 while(heada!=NULL) {6M[heada] =1;7Heada=heada->Next;8 }9 while(headb!=NULL) {Ten if(m[headb]==1)returnheadb; OneHeadb=headb->Next; A } - returnNULL; - } the};View Code
The best solution to the problem, this tip, after traversing list1 and then traversing List2, while traversing list2 and then traversing List1, so that the length of the two traversal is the same as O (n+m), how to judge equality? List1:o o o o⑴⑵⑶list2:----⑴⑵⑶ if list as above, ⑴⑵⑶ for the same node, then traverse List1 This is the case: o o o o⑴⑵⑶----⑴⑵⑶ times This is the case with calendar List2. ----⑴⑵⑶o o o o o⑴⑵⑶ together look: o o o o o⑴⑵⑶----⑴⑵⑶----⑴⑵⑶o o o o⑴ ⑵⑶ Good, now the law comes out. This logic comes out obviously, the main trouble is to traverse one end followed by the second one, directly change the list is not good, so use flag control. Algorithmic logic:
- Determine if the list has a null condition
- Traverse two new linked lists at a time
- If the node address is the same, return
- If it is not the same, continue traversing
- Traversal end returns null
1#include <iostream>2#include <unordered_map>3 using namespacestd;4 5 /**6 * Definition for singly-linked list.7 */8 structListNode {9 intVal;TenListNode *Next; OneListNode (intx): Val (x), Next (NULL) {} A }; - - /** the class Solution { - Public : - ListNode *getintersectionnode (ListNode *heada, ListNode *headb) { - unordered_map<listnode*,int> m; + While (heada!=null) { - M[heada] = 1; + heada=heada->next; A } at While (headb!=null) { - if (m[headb]==1) return headb; - headb=headb->next; - } - return NULL; - } in }; - */ to classsolution{ + Public: -listnode* Getintersectionnode (ListNode *heada,listnode *headb) the { *ListNode * h1=Heada; $ListNode * h2=headb;Panax Notoginseng if(heada==null| | Headb==null)returnNULL; - BOOLflag1=true, flag2=true; the while(heada!=null&&headb!=NULL) { + if(HEADA==HEADB)returnHeada; AHeada=heada->Next; theHeadb=headb->Next; + if(HEADA==NULL&&FLAG1) {heada=h2; Flag1 =false;} - if(HEADB==NULL&&FLAG2) {headb=h1; Flag2 =false;} $ } $ returnNULL; - } - }; the - intMain ()Wuyi { theListNode Head1 (1); -ListNode head2 (2); WuListNode Node1 (3); -ListNode Node2 (4); AboutHead1.next = &Node1; $Node1.next = &Node2; -Head2.next = &Node2; - solution Sol; -ListNode *ret = Sol.getintersectionnode (&head1,&head2); A if(ret==null) cout<<"NULL"<<Endl; + Elsecout<<ret->val<<Endl; the return 0; -}View Code
[Leetcode] Intersection of Linked Lists two linked list intersect