寻找两个链表的相交节点

题目:

单链表可能有环,也可能无环.给定两个单链表的头节点head1和head2,这两个链表可能相交,也可能不相交.请实现一个函数,如果两个链表相交,请返回相交的第一个节点;如果不相交,返回null即可.
要求 : 如果链表1的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外空间复杂度请达到O(1).

解题思路:
这个题可以分为三个题来做.求解此题也是三个步骤.
1-判断是否有环.
    可能两个都无环,也可能两个都有环或者一个有环一个无环.
2-全部无环找到第一个交点.
    两个无环单链表,如果最后一个节点不是同一个节点,则两个链表一定不相交.
3-全部有环分两种情况找到第一个交点.
    全部有环相交分为两种情况:
        a-相交之后入环
            如果两条链表的入环点相同则为相交后入环,求交点的步骤同判断无环链表交点相同.
        b-入环之后相交
            若链表A入环之后的某一个点同链表B的入环点相同,则两个链表为入环后相交,也就是两个链表拥有同一个环但入环点不同.
            此种情况返回两个入环点任意一个即可.
            
说明:两链表一个有环一个无环不可能相交

代码如下:

package com.lwx.sort;

public class FindFirstIntersectionNode {
	public static void main(String[] args) {
		
	}
	
	public static Node getIntersectionNode(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 noLoop(head1,head2);
		}
		
		if(loop1 != null && loop2 != null){
			return bothLoop(head1,head2,loop1,loop2);
		}
		return null;
	}
	/**
	 * 两个有环链表求交点
	 	a-相交之后入环
            如果两条链表的入环点相同则为相交后入环,求交点的步骤同判断无环链表交点相同.
        b-入环之后相交
            若链表A入环之后的某一个点同链表B的入环点相同,则两个链表为入环后相交,也就是两个链表拥有同一个环但入环点不同.
            此种情况返回两个入环点任意一个即可.
	 * @param head1
	 * @param head2
	 * @param loop1
	 * @param loop2
	 * @return
	 */
	public static Node bothLoop(Node head1, Node head2,Node loop1, Node loop2){
		Node cur1 = null;
		Node cur2 = null;
		
		if(loop1 == loop2){
			cur1 = head1;
			cur2 = head2;
			int n = 0;
			while (cur1 != loop1){
				cur1 = cur1.next;
				n++;
			}
			while(cur2 != loop2){
				cur2 = cur2.next;
				n--;
			}
			
			cur1 = n > 0 ? head1 : head2;
			cur2 = head1 == cur1 ? head2 : head1;
			n = Math.abs(n);
			while(n != 0){
				cur1 = cur1.next;
				n--;
			}
			return cur1;
		}else{
			cur1 = loop1.next;
			while(cur1 != loop1){
				if(cur1 == loop2){
					return loop1;
				}
				cur1 = cur1.next;
			}
		}
		
		return null;
	}
	/**
	 * 两个无环链表求交点.
	 * 	若最后一个节点相同则拥有交点.
	 *  两个链表比较一下长度差,长的先走完差值然后两个链表一同next.第一个相同的点为交点.
	 * @param head1
	 * @param head2
	 * @return
	 */
	@SuppressWarnings("unused")
	public static Node noLoop(Node head1, Node head2){
		int n = 0;
		Node temp1 = head1;
		while(temp1 != null){
			temp1 = temp1.next;
			n++;
		}
		
		Node temp2 = head2;
		while(temp2 != null){
			temp2 = temp2.next;
			n--;
		}
		if(temp1 != temp2){
			return null;
		}
		temp1 = n > 0? head1 : head2;
		temp2 = temp1 == head1 ? head2 : head1;
		n = Math.abs(n);
		while(n != 0){
			temp1 = temp1.next;
			n--;
		}
		
		while(temp1 != null){
			temp1 = temp1.next;
			temp2 = temp2.next;
		}
		
		return temp1;
	}
	/**
	 * 获得链表的入环点,无环则返回null
	 * 	两个指针一快一慢一同遍历链表.当快指针等于满指针时,快指针回到开头开始一步一步走.此时快慢指针再相遇的点就是交点.
	 * @param head
	 * @return
	 */
	@SuppressWarnings("null")
	public static Node 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;
			}
			n1 = n1.next;
			n2 = n2.next.next;
		}
		
		n2 = head;
		while(n1 != n2){
			n1 = n1.next;
			n2 = n2.next;
		}
		return n1;
	}
}

class Node{
	int value;
	Node next;
	
	Node(int value){
		this.value = value;
	}
	
	public Node append(Node next){
		if(this.next == null){
			this.next = next;
		}else{
			this.next.append(next);
		}
		return this;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_39445556/article/details/104898342