The algorithm summarizes some of the column problems that intersect two single-linked lists

Source: Internet
Author: User

A single linked list may have a ring, or it may be without a ring. Given two single-linked lists head node Head1 and head2 These two linked lists may or may not be handed in. The implementation function, if intersecting, returns the first node that intersects, not a return null

This topic needs to be analyzed very

The problem of splitting three sub-problems, each sub-problem can be used as a separate algorithm problem.

Question one, how to determine if there is a loop to return the first entry ring node does not return null

Question two, how to determine whether the two non-linked list of the intersection. Intersect returns the first intersecting section, without intersecting returns null

Question three, how to determine whether two linked lists intersect. Intersect returns the first intersecting section, without intersecting returns null

Attention! If one list has rings and the other has no ring, they are impossible to intersect.

If a linked list does not have a loop, it can be traversed again to meet the end of the list. There are rings, then .... Always in the ring .... How to find the first point of entry

thought!!!!! Set a slow pointer to slow and a fast pointer fast start is the head position, and then slow take one step fast walk two steps to traverse

If there is no ring, fast must first meet the end point, once fast arrives at the end, the list is non-ring.

If there is a ring, fast and slow will certainly meet somewhere in the ring, when fast and slow meet, the fast pointer back to the head position, slow not move. Fast moves from two steps to one step at a time, slow is still one step. Continue traversal

Fast and slow are sure to meet again. and meet at the node where the first ring is placed.

Look at the Getloopnode method

 PackageTT;ImportTT. Test84.node; Public classTest99 { Publicnode Getloopnode (node head) {if(head==NULL|| head.next==NULL|| head.next.next==NULL){            return NULL; } Node N1=Head.next; Node N2=Head.next.next;  while(n1!=n2) {            if(N2.next = =NULL|| n2.next.next==NULL){                return NULL; } n2=N2.next; N1=N1.next; } n2=Head;  while(N1! =n2) {N1=N1.next; N2=N2.next; }        returnN1; }            }

If you solve the problem, you know that two linked lists are ring-and-loop-free.

One with a ring and no ring, then the two linked lists are impossible to intersect anyway.

There are two kinds of situations that can intersect: one is no ring, one is a ring.

Problem two judge two non-ring linked list intersect intersection return the first intersection node does not return null

If two non-linked lists intersect then from the intersection node to the two linked list this segment is shared by a linked list.

Solve:

1 list from 1 starting from the head node, go to the last node (not the end) Statistics List 1 length len1 simultaneously records the last node of the linked list 1 end1

2 list from 2 starting from the head node, go to the last node (not the end) Statistics List 2 length len2 simultaneously records the last node of the linked list 1 end2

3 if End1! = End2 shows that two linked lists do not return NULL if the END1=END2 description intersects

4 If the list 1 is longer, the list 1 will go first len1-len2 step.  If the list of 2 is longer, the list 1 will go first len2-len1 step. Then two linked lists go together, and the first one that comes together is what you ask for.

Implementation code:

 PackageTT; Public classTest { Public classnode{ Public intvalue;  PublicNode Next;  PublicNode (intdata) {             This. value=data; }                    }       Publicnode Noloop (node Head1, node head2) {if(head1==NULL|| Head2 = =NULL){          return NULL; } Node Cur1=Head1; Node CUR2=head2; intN =0;  while(Cur1.next! =NULL) {n++; Cur1=Cur1.next; }       while(Cur2.next! =NULL) {n--; CUR2=Cur2.next; }            if(Cur1! =CUR2) {          return NULL; } cur1= n>0?head1:head2; CUR2= Cur1 = = Head1?Head2:head1; N=Math.Abs (n);  while(n!=0) {n--; Cur1=Cur1.next; }             while(Cur1! =CUR2) {Cur1=Cur1.next; CUR2=Cur2.next; }      returnCur1; }      }

Question three, how to determine whether two linked lists Intersect, the intersection returns the first intersection node, the return null

Consider the problem three times, have got two linked list of their first entry point

Topological structure of the judgment:

Let the list 1 from LOOP1, because all the nodes after the LOOP1 are on the ring, so will be able to return to LOOP1 in the future if you go back to Loop1 did not encounter LOOP2 do not want to hand (left)

If you encounter a LOOP2 description intersection (right), return LOOP1 and loop2 at this time

Code:

 PackageTT; Public classTest100 { Public classnode{ Public intvalue;  PublicNode Next;  PublicNode (intdata) {             This. value=data; }        }     Publicnode Bothloop (node Head1, node Loop1, node head2, node Loop2) {node Cur1=NULL; Node CUR2=NULL; if(Loop1 = =loop2) {Cur1=Head1; CUR2=head2; intN=0;  while(Cur1! =LOOP1) {N++; Cur1=Cur1.next; }             while(CUR2! =loop2) {N--; CUR2=Cur2.next; } cur1= n>0?head1:head2; CUR2= Cur1==head1?Head2:head1;  while(n!=0) {n--; Cur1=Cur1.next; }             while(Cur1! =CUR2) {Cur1=Cur1.next; CUR2=Cur2.next; }            returnCur1; }Else{Cur1= Loop1.next;//in-loop test            while(Cur1! =LOOP1) {               if(cur1==loop2) {                   returnLoop1; } cur1=Cur1.next; }            return NULL; }    }        }

All code:

 PackageTT; Public classTest101 { Public classnode{ Public intvalue;  PublicNode NextNode;  PublicNode (intdata) {             This. value=data; }    }         Publicnode Getintersectnode (node Head1, node head2) {if(Head1 = =NULL|| head2==NULL){            return NULL; } Node Loop1= Getloopnode (HEAD1);//Get the Ring nodeNode LOOP2 =Getloopnode (head2); if(loop1==NULL&& loop2==NULL){            returnNoloop (Head1, head2); }        if(Loop1! =NULL&& LOOP2! =NULL){            returnBothloop (Head1, LOOP1, Head1 loop2); }        return NULL; }                    }

The algorithm summarizes some of the column problems that intersect two single-linked lists

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.