两个单链表相交的一系列问题

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

分析:要想求解这个问题先要解决一个基本问题,怎么判断一个链表是有环还是无环?再进一步,如果一个链表有环,请返回它第一个入环的节点。如果一个链表无环,返回空。

方法一:利用哈希表,每个节点在遍历的过程中,把每个节点加到hashset里面去,没有value,只有key,如果有环,就会重复回到某个节点,而每一个节点遍历时都要加到set里面去,所以如果有环,就会发现某个节点之前遍历过,如果走到空,就无环。

public static Node getfirstNode(Node head){
    HashSet<Node> set = new Hash<Node>();
    while(head != null){
        if(set.contains(head)){
            return head;
        }
        set.add(head);
        head = head.next;
    }
    return null;
}

方法二:不用哈希表。对于一个链表,准备两个指针,一个叫快指针F,一个叫慢指针S,快指针F一次走两步,慢指针S一次走一步,如果走的过程中,快指针走到了空,那就肯定无环;反之如果有环,快指针和慢指针一定会在环上相遇(自己画一画就能看出来)。这里的相遇不是指值相等,而是指内存地址是同一个。相遇之后,快指针回到开头,变成每次走一步,接下来快指针和慢指针一起走,他们一定会在第一个入环节点处相遇。(这也是一个数学结论,在此不作证明了)。时间复杂度是O(N)。

这个过程也就是如果一个链表有环,返回第一个入环节点;如果这个链表无环,返回空。

public static Node getLoopNode(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		Node n1 = head.next; // n1 -> slow
		Node n2 = head.next.next; // n2 -> fast
		while (n1 != n2) {
			if (n2.next == null || n2.next.next == null) { //判断快指针是否走到空
				return null;
			}
			n2 = n2.next.next;
			n1 = n1.next;
		}
		n2 = head; // n2 -> walk again from head 快指针回到头
		while (n1 != n2) {
			n1 = n1.next;
			n2 = n2.next;
		}
		return n1;
	}

接下来解决相交的问题,已经有了上面这个loop函数,有两个链表,链表1的头节点是head1,链表2的头节点是head2,调用loop函数之后就可以得到链表1的第一个入环节点loop1,得到链表2的第一个入环节点loop2。

情况一(两个无环链表相交):如果loop1==null,loop==null,这就是两个无环链表的相交问题(要么不相交,要么相交),这时候可以用hash表来做,hash表先遍历链表1,把链表1的所有节点都加到hash表里去,然后在遍历链表2的过程中,如果发现有任何一个节点已经存在hash表中,就是和链表1第一个相交的节点。

不用hash表的话,先遍历链表1,由于链表无环,可以一直走到结尾,统计链表1的长度并找到链表1的最后一个节点e1,再遍历链表2,统计链表2的长度并找到链表2的最后一个节点e2,如果e1和e2不是同一个节点,那么链表1和链表2不可能相交,返回空。(一定是看最后一个节点,因为单链表不可能形成那种交叉结构)。如果e1=e2,说明两个链表肯定相交。长的链表先走差值步,然后和短链表一起走,他俩一定会共同进入第一个入环节点。

public static Node noLoop(Node head1, Node head2) {
		if (head1 == null || head2 == null) {
			return null;
		}
		Node cur1 = head1;
		Node cur2 = head2;
		int n = 0;
		while (cur1.next != null) {
			n++;
			cur1 = cur1.next;
		}
		while (cur2.next != null) {
			n--;
			cur2 = cur2.next;
		}
		if (cur1 != cur2) {
			return null;
		}
		cur1 = n > 0 ? head1 : head2;
		cur2 = cur1 == head1 ? head2 : head1;
		n = Math.abs(n);
		while (n != 0) {
			n--;
			cur1 = cur1.next;
		}
		while (cur1 != cur2) {
			cur1 = cur1.next;
			cur2 = cur2.next;
		}
		return cur1;
	}

情况二(一个链表有环,一个链表无环):如果一个链表有环,一个链表无环,必然不会相交。

情况三(两个链表都有环):两个链表都有环,就是loop1 != null,loop2 != null,会形成三种结构。

                   

怎么判断是这三种中的哪一种呢?由h1得到第一个入环节点loop1,由h2得到第二个入环节点loop2,如果loop1=loop2,就是第二种结构,要求得第一个相交的节点,把环里的部分忽略掉,把loop1作为终止,loop2作为终止,就又成了一个无环链表的相交问题。如果loop1不等于loop2,就是第一种或者第三种结构。此时让loop1继续往下走,因为loop1是第一个入环节点,它往下走一定能回到自己,如果loop1回到自己都还没有遇到loop2,就是第一种结构,如果loop1往下走的过程中遇到了loop2,就是第三种结构。如果遇到loop2,返回loop1作为第一个相交节点或者返回loop2作为第一个相交的节点,都对(loop1离h1更近,loop2离h2更近)。整个过程结束。整体的代码如下:

public class FindFirstIntersectNode {

	public static class Node {
		public int value;
		public Node next;

		public Node(int data) {
			this.value = data;
		}
	}

	public static Node getIntersectNode(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, loop1, head2, loop2);
		}
		return null;
	}

	public static Node getLoopNode(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		Node n1 = head.next; // n1 -> slow
		Node n2 = head.next.next; // n2 -> fast
		while (n1 != n2) {
			if (n2.next == null || n2.next.next == null) {
				return null;
			}
			n2 = n2.next.next;
			n1 = n1.next;
		}
		n2 = head; // n2 -> walk again from head
		while (n1 != n2) {
			n1 = n1.next;
			n2 = n2.next;
		}
		return n1;
	}

	public static Node noLoop(Node head1, Node head2) {
		if (head1 == null || head2 == null) {
			return null;
		}
		Node cur1 = head1;
		Node cur2 = head2;
		int n = 0;
		while (cur1.next != null) {
			n++;
			cur1 = cur1.next;
		}
		while (cur2.next != null) {
			n--;
			cur2 = cur2.next;
		}
		if (cur1 != cur2) {
			return null;
		}
		cur1 = n > 0 ? head1 : head2;
		cur2 = cur1 == head1 ? head2 : head1;
		n = Math.abs(n);
		while (n != 0) {
			n--;
			cur1 = cur1.next;
		}
		while (cur1 != cur2) {
			cur1 = cur1.next;
			cur2 = cur2.next;
		}
		return cur1;
	}

	public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
		Node cur1 = null;
		Node cur2 = null;
		if (loop1 == loop2) {
			cur1 = head1;
			cur2 = head2;
			int n = 0;
			while (cur1 != loop1) {
				n++;
				cur1 = cur1.next;
			}
			while (cur2 != loop2) {
				n--;
				cur2 = cur2.next;
			}
			cur1 = n > 0 ? head1 : head2;
			cur2 = cur1 == head1 ? head2 : head1;
			n = Math.abs(n);
			while (n != 0) {
				n--;
				cur1 = cur1.next;
			}
			while (cur1 != cur2) {
				cur1 = cur1.next;
				cur2 = cur2.next;
			}
			return cur1;
		} else {
			cur1 = loop1.next;
			while (cur1 != loop1) {
				if (cur1 == loop2) {
					return loop1;
				}
				cur1 = cur1.next;
			}
			return null;
		}
	}

	public static void main(String[] args) {
		// 1->2->3->4->5->6->7->null
		Node head1 = new Node(1);
		head1.next = new Node(2);
		head1.next.next = new Node(3);
		head1.next.next.next = new Node(4);
		head1.next.next.next.next = new Node(5);
		head1.next.next.next.next.next = new Node(6);
		head1.next.next.next.next.next.next = new Node(7);

		// 0->9->8->6->7->null
		Node head2 = new Node(0);
		head2.next = new Node(9);
		head2.next.next = new Node(8);
		head2.next.next.next = head1.next.next.next.next.next; // 8->6
		System.out.println(getIntersectNode(head1, head2).value);

		// 1->2->3->4->5->6->7->4...
		head1 = new Node(1);
		head1.next = new Node(2);
		head1.next.next = new Node(3);
		head1.next.next.next = new Node(4);
		head1.next.next.next.next = new Node(5);
		head1.next.next.next.next.next = new Node(6);
		head1.next.next.next.next.next.next = new Node(7);
		head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

		// 0->9->8->2...
		head2 = new Node(0);
		head2.next = new Node(9);
		head2.next.next = new Node(8);
		head2.next.next.next = head1.next; // 8->2
		System.out.println(getIntersectNode(head1, head2).value);

		// 0->9->8->6->4->5->6..
		head2 = new Node(0);
		head2.next = new Node(9);
		head2.next.next = new Node(8);
		head2.next.next.next = head1.next.next.next.next.next; // 8->6
		System.out.println(getIntersectNode(head1, head2).value);

	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值