Determine if two linked lists intersect and find the intersection __java

Source: Internet
Author: User
Tags comparable

A classic question that determines whether two linked lists Intersect, and if they intersect, find their intersections.

Ideas:

1, encounter this problem, the first impression is to use hash to judge, the two linked list of nodes to hash, and then determine the node, this idea of course can be.

2, of course, the use of violence is also possible, traversing two linked lists, in the process of traversing the comparison, to see if the node is the same.

3, the third way of thinking is more peculiar, in the beauty of programming to see. First loop through the first list to his tail, and then point the trailing next pointer to the second list (the tail pointer's next is pointing to null). This two linked list to synthesize a linked list, to determine whether the original two linked lists intersect also changed to determine whether the new linked list has a ring problem: that is, to determine whether a single linked list has a ring.

This conversion can be judged from the head of the linked list, but it is not used. It is easy to know by simple understanding that if the new linked list is ring, then the head of the second linked list must be on the ring. So we can iterate through the head of the second list, thus reducing the time complexity (the reduced time complexity is the length of the first linked list).

The following figure is a simple demo:

This method can determine whether two linked lists intersect, but it is not easy to find their intersections.

4, carefully study the two linked lists, if they intersect, then their last node must be the same, otherwise it will not intersect. Therefore, it is easy to determine whether two linked lists intersect, traversing to the tail of two lists, and then judging whether they are the same, if the same, then intersect; The schematic diagram is as follows:

The two lists intersect to determine their intersection point. Assuming that the first list length is len1, the second asks Len2, and then finds the longer length of the linked list pointer to move backward |len1-len2| (The absolute value of the len1-len2), and then start traversing two lists to determine if the node is the same.

Nodes of a single linked table
Class Node<t extends comparable<t>> implements comparable<node<t>>{
Private T data;
Private node<t> Next;

Public Node () {

}
Public Node (T data) {
This.data=data;
This.next=null;
}



Public T GetData () {
return data;
}
public void SetData (T data) {
This.data = data;
}
Public node<t> GetNext () {
return to Next;
}
public void Setnext (node<t> next) {
This.next = Next;
}
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + ((data = null)? 0:data.hashcode ());
return result;
}


@Override
public boolean equals (Object obj) {
if (this = obj)
return true;
if (obj = null)
return false;
if (GetClass ()!= obj.getclass ())
return false;
node other = (node) obj;
if (data = = NULL) {
if (other.data!= null)
return false;
else if (!data.equals (Other.data))
return false;
return true;
}
@Override
public int compareTo (node<t> o) {
Return This.data.compareTo (O.getdata ());
}
}



Determine if two single linked lists intersect
If a simple judgment intersects, just see if the last pointer is equal
/*
* A total of two solutions
* First: Connect two linked lists to see if there is a ring in List 2. If there is a ring, the first entry point is the point of intersection
*
* */
A single linked list is not a ring
/*
* Two The nature of the Intersect of a list
* (1) Once the two lists intersect, then the nodes in the two list must have the same address.


(2) Once the two lists intersect, then two lists from the intersection node to the tail node must all be the same node.
* Solving Ideas
* First go through the first list to his tail, and then point the trailing next pointer to the second list (the tail pointer's next is pointing to null).
* So two linked lists to synthesize a linked list, to determine whether the original two linked lists intersect also changed to determine whether the new linked list has a ring problem: that is, to determine whether a single linked list has a ring
* */
Public node<t> isintersectbyconnect (node<t> head1,node<t> head2) {
Get the last node of the current list
Node<t> Target=null;
if (head1!=null) {
while (Head1.getnext ()!=null) {
Head1=head1.getnext ();
}
Head1.setnext (HEAD2);
Target=head1;
}else if (head2!=null) {
while (Head2.getnext ()!=null) {
Head2=head2.getnext ();
}
Head2.setnext (HEAD1);
Target=head2;
}
return this. Findloopport (HEAD2);
}

Public node<t> isintersect (node<t> head1,node<t> head2) {
Node<t> Target=null;
if (head1==null| | Head2==null) return target;
Boolean pos1=this.hascycle (HEAD1);
Boolean pos2=this.hascycle (HEAD2);
A linked list has a ring, another list has no ring, and there must be no intersection.
if (pos1==false&&pos2==true| | POS1==FALSE&AMP;&AMP;POS2==TRUE) return null;
int len1=this.size (HEAD1);
int len2=this.size (HEAD2);
if (len1>=len2) {
for (int i=0;i<len1-len2;i++) {
Head1=head1.getnext ();
}
}else{
for (int i=0;i<len2-len1;i++) {
Head2=head2.getnext ();
}
}
while (Head1!=null&&head2!=null) {
if (Head1.equals (head2)) {
Target=head1;
Break
}
else{
Head1=head1.getnext ();
Head2=head2.getnext ();
}
}
return target;
}

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.