Data structure and algorithm surface test questions 80 (7)

Source: Internet
Author: User
Tags diff

question No. 7

Programming of the Microsoft Institute of Asia

Determine if two linked lists Intersect

Give two one-way lists of head pointers, such as H1,H2, to determine whether the two linked lists intersect.

To simplify the problem, we assume that both of the lists are not with loops.

Problem extension :

1. If the linked list may have ring out ?

2. What if you need to find the first node column where two linked lists intersect ?

Ideas:

Determines whether two linked lists intersect and asks for the first node column (no loop).

Utilization Count

If the two linked lists intersect, the two linked lists will have a common node column, the length of the list of links, to find two linked list length difference (The purpose is to change the two linked list to the end of the same length of the two linked list, the intersection part must be inside), and then traverse backward, find the same node, is the first node.

Determine if there is a ring

can set two pointers (Fast,slow), the initial values are pointed to the head, slow each step forward, fast each step forward two steps, if there is a ring in the list, then fast first into the ring, and after slow into the ring, two pointers in the ring must meet, if fast traverse to the tail is null, is no ring. the entry point for the ring   when fast and slow meet, slow certainly did not walk through the list, and fast has been circulating in the loop N-Circle (1<=n). Assuming that slow took the S-step, fast took 2s steps (the fast step is also equal to S plus the N-turns on the ring), the ring length is R, then:

2s = s + nr
s= NR

Set the entire chain table length L, the inlet ring and meet point distance is X, the distance from the starting point to the ring entry is a.
A + x = NR
A + x = (n–1) r +r = (n-1) R + l-a
A = (n-1) R + (l–a–x)

(l–a–x) =s is the distance from the point of encounter to the ring entry point, so from the chain head to the ring entry point equals (n-1) loop inner ring + meet point to ring entry pointThus, you can set a pointer at the end of the list, the meeting point, each one step at a time, two pointers must meet, then meet the 1th ring entry point. whether a linked list with a ring intersects   (1) The linked list is divided into two linked lists, one is the ring segment, and the other is the entrance to the ring (including the entry point). (2) compare the paragraph without ring. Method is the same as comparing a non-ring linked list. The following code gives a sense of whether there is a ring, as well as the entry point of the ring lookup, if the lookup is null, the description is no ring, if there is a ring, you can write a function to break the list from the ring (this function is not given).

#include <cstdio>#include<malloc.h>#include<iostream>using namespacestd;structnode{intM_nvalue; List*next;};voidInsertlist (Node *&head,intvalue) {    if(head==NULL) {List*m_plist=NewList (); if(m_plist==NULL) {cout<<"Insufficient memory.\n"; return; } m_plist->m_nvalue=value; M_plist->next=NULL; Head=m_plist; }ElseInsertlist (head->next,value);}
Find the intersection point node* FindNode (Node *head1,node *head2) { if(head1==null| | Head2==null)returnNULL;//if it is empty, it must not intersectNode *p1,*P2; P1=head1;p2=head2; intlen1=0, len2=0; intdiff=0;//count their length differences by counting methods while(p1->next!=NULL) {P1=p1->next;len1++; } while(p2->next!=NULL) {P2=p2->next;len2++; } if(P1!=P2)returnNULL;//The last node is different, definitely not intersect .diff=len1>len2?len1-len2:len2-len1; if(LEN1&GT;LEN2) {p1=head1;p2=head2;} Else{p1=head2;p2=Head1;} for(intI=0; i<diff;i++) p1=p1->Next; while(p1!=p2) {P1=p1->next;p2=p2->Next; } returnP2; }node* Hasloop (Node *head) { BOOLHasloop =false; Node*fast,*slow;//using fast and slow pointersfast=head;slow=Head; while(fast&&fast->next) {Slow=slow->Next; Fast=fast->next->next;//fast every two steps, slow one step at a time . if(Fast==slow) {//when the two hands meet, it means that there is a ringHasloop =true; Break; } } if(Hasloop) {//when we have a ring, we find the first node in the ring.slow=Head; while(slow!=fast) {Slow=slow->Next; Fast=fast->Next; }
You can call the Decomposition function of the list here. The slow is set as a flag, and the new linked list with no loops is header to slow. slow->next=null;
  return slow;

} Else return NULL;}

Data structure and algorithm surface test questions 80 (7)

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.