給定兩個鏈表的頭結點,返回它們相交的第一個節點,若是不相交,則返回null。注意:兩個鏈表均可能含有環。node
時間複雜度O(N),空間複雜度O(1)。oop
package com.iqiyi;
import org.junit.Assert;
import org.junit.Test;
public class Code2_11 {
public static class Node{
int data;
Node next;
Node(int data){
this.data=data;
}
}
public static Node getLoopNode(Node node){
if(node==null||node.next==null||node.next.next==null)
return null;
Node slow=node.next;
Node fast=node.next.next;
while(slow!=fast){
if(fast.next==null||fast.next.next==null)
return null;
slow=slow.next;
fast=fast.next.next;
}
fast=node;
while(fast!=slow){
fast=fast.next;
slow=slow.next;
}
return fast;
}
public static Node getNoLoop(Node head1,Node head2){
Node temp1=head1;
int length1=0;
while(temp1.next!=null){
temp1=temp1.next;
length1++;
}
Node temp2=head2;
int length2=0;
while(temp2.next!=null){
temp2=temp2.next;
length2++;
}
if(temp1!=temp2)
return null;
Node c1=null;
Node c2=null;
int d=0;
if(length1>length2){
c1=head1;
c2=head2;
d=length1-length2;
}
else{
c1=head2;
c2=head1;
d=length2-length1;
}
while(d>0){
c1=c1.next;
d--;
}
while(c1!=c2){
c1=c1.next;
c2=c2.next;
}
return c1;
}
public static Node getBothLoop(Node head1,Node loop1,Node head2,Node loop2){
if(loop1==loop2){
Node temp1=head1;
int length1=0;
while(temp1.next!=loop1){
temp1=temp1.next;
length1++;
}
Node temp2=head2;
int length2=0;
while(temp2.next!=loop2){
temp2=temp2.next;
length2++;
}
Node c1=null;
Node c2=null;
int d=0;
if(length1>length2){
c1=head1;
c2=head2;
d=length1-length2;
}
else{
c1=head2;
c2=head1;
d=length2-length1;
}
while(d>0){
c1=c1.next;
d--;
}
while(c1!=c2){
c1=c1.next;
c2=c2.next;
}
return c1;
}
else{
while(loop1.next!=loop1){
if(loop1==loop2)
return loop1;
loop1=loop1.next;
}
return null;
}
}
public static Node getIntersectantNode(Node head1,Node head2){
if(head1==null||head2==null)
return null;
Node loop1=getLoopNode(head1);
Node loop2=getLoopNode(head2);
if(loop1==null&&loop2==null)
return getNoLoop(head1, head2);
if(loop1!=null&&loop2!=null)
return getBothLoop(head1, loop1, head2, loop2);
return null;
}
@Test
public void test1(){
Node node0=new Node(1);
Node node1=new Node(2);
Node node2=new Node(3);
Node node3=new Node(4);
Node node4=new Node(5);
Node node5=new Node(6);
Node node6=new Node(7);
Node node7=new Node(8);
node0.next=node1;
node1.next=node2;
node2.next=node3;
node3.next=node4;
node4.next=node5;
node5.next=node6;
node6.next=node7;
node7.next=node3;
Node node8=new Node(11);
Node node9=new Node(10);
Node node10=new Node(9);
node8.next=node9;
node9.next=node10;
node10.next=node2;
Node ans=getIntersectantNode(node0, node8);
Assert.assertNotNull(ans);
Assert.assertTrue(ans.data==3);
}
}
複製代碼