您的位置:首页 > 其它

两链表相交问题

2017-12-08 21:40 197 查看
/*两链表相交问题
1.一个有环一个无环,则不相交
2.两个都无环{有两种情况:有无交点}
3.两个都有环{
        1环入口结点相同
        2环入口结点不同(有两种情况:有无交点)}*/
public static Node isIntersect(Node head1,Node head2){
if(head1==null||head2==null)return null;
Node loop1=isloop(head1);
Node loop2=isloop(head2);
if(loop1==null&&loop2!=null||loop2==null&&loop1!=null){//情况一
return null;
}else if(loop1==null&&loop2==null){//情况二
return(noloop(head1,head2));			 
}else{
return (twolooplist(head1,head2,loop1,loop2));//情况三
}
}

public static Node isloop(Node head){//链表是否有环,有则返回环入口结点
Node first=head.next;//first在second后面原因:防止在环之前两者相等
Node second=head;
while(first.next!=null&&first.next.next!=null){
if(first==second){
return first;
}
first=first.next.next;
second=second.next;
}
return null;
}
public static Node noloop(Node head1,Node head2){
int len1=0,len2=0;
Node p1=head1,p2=head2;
while(p1.next!=null){
p1=p1.next;
len1++;
}
while(p2.next!=null){
p2=p2.next;
len2++;
}
if(p1!=p2) return null;//两链表尾结点不同,不相交
else{					//长的链表先走长的部分,之后一起走
p1=head1;
p2=head2;
if(len1>=len2){
for(int i=0;i<(len1-len2);i++){
p1=p1.next;
}
while(p1!=null&&p2!=null){
if(p1==p2)return p1;
p1=p1.next;
p2=p2.next;
}
}else{
for(int i=0;i<(len2-len1);i++){
p2=p2.next;
}
while(p1!=null&&p2!=null){
if(p1==p2)return p1;
p1=p1.next;
p2=p2.next;
}
}
return null;
}
}
private static Node twolooplist(Node head1, Node head2,Node loop1,Node loop2) {
if(loop1==loop2) {			//入环结点相同,交点可能在入环结点之前,长链表应先走一段
Node cur1=loop1;
Node cur2=loop2;
int n=0;
while(cur1!=loop1){
cur1=cur1.next;
n++;
}
while(cur2!=loop2){
cur2=cur2.next;
n--;
}
cur1=n>0?head1:head2;
cur2=cur1==head1?head2:head1;
n=Math.abs(n);
for(int i=0;i<n;i++){
cur1=cur1.next;
}
while(cur1!=cur2){
cur1=cur1.next;
cur2=cur2.next;
}
return cur1;
}else{					//入环结点不同
Node q=loop1.next;
while(q!=loop1){
if(q==loop2)return loop1;//从一入环结点出发直到碰到另一入环结点
q=q.next;
}
return null;	//两环无相交
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: