题目描述:
在本题中,单链表可能有环,也可能无环。给定两个单链表的头节点 head1和head2,这两个链表可能相交,也可能不相交。请实现一个函数, 如果两个链表相交,请返回相交的第一个节点;如果不相交,返回null 即可。 要求:如果链表1的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外空间复杂度请达到O(1)。
public class Code_022_FindFirstIntersectNode {
public static class Node {
public int data;
public Node next;
public Node(int data) {
this.data = 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;
}
//判断一个链表是否有环,如果有返回入环的第一个节点,否则返回null
public static Node getLoopNode(Node head) {
if (head == 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;
}
//判断两个无环链表是否相交,如果相交返回相交的第一个节点,否则返回null
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
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;
}
//判断两个有环链表是否相交,如果相交返回相交的第一个节点,否则返回null
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) {
int n = 0;
cur1 = head1;
cur2 = head2;
while(cur1 != loop1) {
n++;
cur1 = cur1.next;
}
while(cur2 != loop2) {
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;
} else {
cur1 = loop1.next;
while(cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
}