Interview question-determine whether the linked list is intersecting

Source: Internet
Author: User

Today, I read a typical July article "Programmer programming Art: Chapter 9" catch up with gossip linked lists ", because I have been reviewing the algorithms related to data structures. By the way, learn how to determine whether the linked list of July is intersecting. Source:Http://blog.csdn.net/v_JULY_v.

Question: The head pointer of two one-way linked lists is given to determine whether or not they are intersecting.

The preceding solution of July is as follows:

Analysis:This is an interview question from the Emy of Microsoft in the beauty of programming. Please follow my ideas step by step (some text comes from the beauty of programming ):

  1. Judge whether each node of the first linked 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.
  2. Create a hash table for the first linked list, query the hash table, and determine whether each node of the second linked list appears in the hash table, if all the nodes of the second linked list can be found in the hash table, it means that the second linked list has the same node with the first linked list. The time complexity is linear: O (length (H1) + Length (H2). to store all nodes in the first linked list, the space complexity is O (length (H1 )). Is there a better way to solve problems with linear time complexity and reduce storage space?
  3. To further consider this feature, we can know that if two linked lists without loops overlap with a node, then all nodes after this node are shared by two linked lists, then the last node must be in total. However, we can easily get the last node of the linked list, which becomes a major breakthrough in simplifying the solution. Then, we only need to judge whether the tail pointers of the two linked lists are equal. Otherwise, the linked list does not.
    Therefore, traverse the first linked list first and remember the last node. Then traverse the second linked list and compare it with the last node of the first linked list. If the two nodes are the same, they will be the same. Otherwise, they will not be the same. In this way, we get a time complexity, which is O (length (H1) + Length (H2), and uses only one additional pointer to store the last node. The time complexity of this method is linear O (n), and the space complexity is O (1), which is obviously better than solution 3.
  4. The above problems are all aimed at linked list loops,What if the linked list has Loops now?Can we still find the last node for determination? Is the above method equally valid? Obviously, the nature of this problem has been transformed into determining whether a linked list has loops. So how can we determine whether a linked list has a ring?

Summary:
Therefore, in fact, the question of determining whether two linked lists are at the same intersection is transformed:
1. First, judge whether a belt or no ring exists.
2. If none of them contain loops, determine whether the end node is equal.
3. If both nodes contain loops, determine the node on which the two pointers meet on a linked list. The node is not on the other linked list.
If yes, It is intersecting. If not, it is not.

Next we will first show whether there is a ring algorithm. We will use the method in the previous article. However, the question needs to be added with the last node saved.

The algorithm is the same as the July method, but the implementation method is a little different.

// Determine whether the linked list has a ring bool isloop (node * head, node ** firstloop, node ** lastnode) {node * First = head; node * Second = head; if (null = head | null = head-> next) {return false;} do {First = first-> next; // first take one step second = Second-> next; // second take two steps} while (second & Second-> next & first! = Second); If (first = Second) {* firstloop = first; // return true for the first node of the Save loop;} else {* lastnode = first-> next; // Save the End Node // printf ("% d \ n", first-> next-> data); Return false;} return false ;}

The next step is to determine whether the intersection is an algorithm. It is the same as the July code, but I wrote it myself. Learning .....

// Determine whether two linked lists are intersecting. // 1. if none of them contain loops, determine whether the end node is equal. // 2. if both nodes contain loops, judge the node where the two pointers meet on a linked list, and the node is not on the other linked list. Bool iscross (node * head1, node * head2) {node * firstloop1 = NULL; node * firstloop2 = NULL; node * lastnode1 = NULL; node * lastnode2 = NULL; if (null = head1 | null = head2) {printf ("link is empty! \ N "); exit (1) ;}bool isloop1 = isloop (head1, & firstloop1, & lastnode1); bool isloop2 = isloop (head2, & firstloop2, & lastnode2 ); if (! Isloop1 &&! Isloop2) // both have no loops {return (lastnode1 = lastnode2);} else if (isloop1! = Isloop2) // One has a ring and the other has a ring {return false;} else // both have a ring, determine whether the node in the ring can reach the node {node * temp = firstloop1-> next; while (temp! = Firstloop1) {If (temp = firstloop2) // determines whether the link points in the second linked list return true in the first linked list; temp = temp-> next ;}} return false ;}

Thanks to July's classic article ..






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.