两链表相交问题

来源:互联网 发布:引导弹升级数据 编辑:程序博客网 时间:2024/06/08 17:11
/*两链表相交问题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;//两环无相交}}
原创粉丝点击