1 assuming that two linked lists have no loops
Thinking of solving problems
A. The direct loop determines whether each node in the first list is in the second linked list. However, the time complexity of this method is O (Length (H1) * Length (H2)). Obviously, we have to find a more effective method, at least not the complexity of O (n^2).
B. Construct the hash table directly for the first linked list, then query the hash table to determine if each node of the second list appears in the hash table, and if all the nodes of the second linked list can be found in the hash table, That means the second list has the same node as the first linked list. The time complexity is linear:O (Length (H1) + length (H2)), and in order to store all nodes of the first linked list, the spatial complexity is o (length (H1)). Is there a better way to solve problems with linear time complexity and to reduce storage space?
C. The issue of conversion to a ring. The second linked list is followed by the first linked list, and if the resulting list has loops, then the two linked lists intersect. The question of how to determine the ring has already been discussed, but there are simpler ways. Because if there is a ring, then the table header of the second list must also be on the ring, that is, the second list will form a circular linked list, we just need to traverse the second list to see if it will return to the starting point can be judged. The time complexity of this method is linear and the space is constant.
D. Further consideration "if two non-ring linked lists intersect a node, then all nodes after this node are all two linked lists " This feature, we can know that if they intersect, then the last node must be common. And we can easily get the last node of the list, so this is a major breach of our simplified solution. So, let's just determine if the tail pointers of the two lists are equal. is equal, the linked list intersects; otherwise, the list does not intersect.
So, the first list is traversed, and the last node is remembered. The second list is then traversed, and the last node is compared to the last node of the first list, and if the same, it intersects, otherwise, does not intersect. So we get a time complexity, which is O ((Length (H1) + length (H2)), and only an extra pointer is used to store the last node. The time complexity of this method is Linear O (N), the spatial complexity is O (1), obviously better than the solution shift
2 Assuming that two lists have loops, there are only 2 cases: intersect on "Ring" or intersect in "not part of ring". So the ring must be on the public part. If you know any of the nodes on one of the linked lists, you just need to determine if you are on the other list. Is there a question here? In the general sense, there is no such form of linked list: 1->2->3->2->4?
//To determine if a linked list intersects without a ring//2. If you do not have a ring, determine whether the tail node is equal//3. If you have a ring, judge the node on the list where the two pointers meet, not on the other linked list. BOOLDetect (Node * head1, Node *head2) {Node*CircleNode1; Node*CircleNode2; Node*LastNode1; Node*LastNode2; BOOLIsCircle1 =iscircle (Head1,circlenode1, lastNode1); BOOLIsCircle2 =iscircle (Head2,circlenode2, LastNode2); //one with ring, one without ring if(IsCircle1! =isCircle2)return false; //two are not ring, to determine whether the last node is equal Else if(!iscircle1 &&!)isCircle2) { returnLastNode1 = =LastNode2; } //all two have rings to determine if the node in the ring can reach the node in the other chain . Else{Node* Temp = circlenode1->next;//updated, thanks to the Wolf and Hyy. while(Temp! =circleNode1) { if(temp = =circleNode2)return true; Temp= temp->Next; } return false; } return false; }
3 Finding the first element to intersect
For 1 intersect, as long as you know two linked list length L1 and L2, then let the long one go first abs (L1-L2), and then go together, the same node is the first element
//find the first common node that intersects two linked listsnode* Findintersectnode (Node *h1,node *H2) { intLen1 = Listlength (H1);//To find the chain table length intLen2 =listlength (H2); //align two linked lists if(Len1 >len2) { for(intI=0; i<len1-len2;i++) H1=h1->Next; } Else { for(intI=0; i<len2-len1;i++) H2=h2->Next; } while(H1! =NULL) { if(H1 = =H2)returnH1; H1= h1->Next; H2= h2->Next; } returnNULL;}
http://wuchong.me/blog/2014/03/25/interview-link-questions/
http://blog.csdn.net/v_july_v/article/details/6447013
Determine if two linked lists Intersect