题目:
给定两个可能有环也可能无环的单链表。实现一个函数,如果两个链表相交,请返回相交的第一个节点;不相交则返回null。如果两个链表长度之和为N,要求时间复杂度为O(N),额外空间复杂度为O(1)
分析:
1.首先确定两个链表是否有环(使用快慢指针方法)
2.当两个链表都为无环结构:
如果两个链表相交,则相交后的部分一定是公共部分,因为如果相交后再出现分叉,则说明相交节点有两个next指针,不符合实际
并且注意假设长链表为1->2->3->4->null,短链表为1->3->null,尽管有一个相同的节点,但是不属于链表相交,因为节点3在
两个链表中的next不一致,所以相交链表的最后一个节点一定是同一个节点
\ \ /
--------- not -------
/ / \
① 找到两个链表的end节点以及各自的长度,判断end节点是否一致,不一致就不相交,一致就相交
② 计算两个链表长度的差值,长链表先移动差值步。然后两个链表同时移动,最终一定会同时移动到相交节点处,并返回相交节点
3.当一个链表有环,一个无环:这种情况不可能相交,因为如果相交必定有一个节点有两个next指针
4.当两个链表都有环,需要分为三种情况:
4.1 两个链表不相交
4.2 两个链表相交,且入环节点为同一个节点
-----
\ / \
-----·
/ \ /
-----
这种情况只需要把成环部分剔除,将前半部分视为两个无环链表相交的处理方式(即noLoop函数)
4.2 两个链表相交,但是入环节点不为同一个节点
-----
\ / \ /
-----1 2------
\ /
-----
此时令其中一个入环节点沿着环开始移动,如果遇到另一个入环节点,则说明符合情况4.2(返回1或2皆可)
如果走了一圈发现没遇到,则为情况4.1
代码:
public class FindFirstIntersectNode {
public static class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
// 判断链表是否有环并返回第一个入环节点 方法1:遍历节点并将节点存入Set中
public static Node getLoopNode1(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Set<Node> set = new HashSet<>();
while (head != null) {
if (set.contains(head)) {
return head;
} else {
set.add(head);
head = head.next;
}
}
return null;
}
// 判断链表是否有环并返回第一个入环节点 方法2:使用快慢指针
public static Node getLoopNode2(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
// 为什么不将两个指针初始设置指向head?如果初始化为head,则下面的while循环无法执行
Node n1 = head.next; // 慢指针,一次移动一步
Node n2 = head.next.next; // 快指针,一次移动两步
// 两个指针相遇时停止循环
while (n1 != n2) {
// 出现next指针指向null的情况就说明该链表无环
if (n2.next == null || n2.next.next == null) {
return null;
}
n2 = n2.next.next;
n1 = n1.next;
}
// 将快指针重新放回到head处
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;
}
Node cur1 = head1;
Node cur2 = head2;
int len1 = 0; // 链表1的长度
int len2 = 0; // 链表2的长度
while (cur1.next != null) {
len1++;
cur1 = cur1.next;
}
while (cur2.next != null) {
len2++;
cur2 = cur2.next;
}
// 如果两个链表的尾节点不一致,则返回null
if (cur1 != cur2) {
return null;
}
cur1 = (len1 - len2) > 0 ? head1 : head2; // 哪个链表长度较长,那个链表的头变为cur1
cur2 = cur1 == head1 ? head2 : head1; // 哪个链表长度较短,那个链表的头变为cur2
int n = Math.abs(len1 - len2);
// 长链表先移动差值步
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;
// 此时对应4.2
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int len1 = 0; // 链表1非环部分的长度
int len2 = 0; // 链表2非环部分的长度
while (cur1 != loop1) {
len1++;
cur1 = cur1.next;
}
while (cur2 != loop2) {
len2++;
cur2 = cur2.next;
}
cur1 = (len1 - len2) > 0 ? head1 : head2; // 哪个链表长度较长,那个链表的头变为cur1
cur2 = cur1 == head1 ? head2 : head1; // 哪个链表长度较短,那个链表的头变为cur2
int n = Math.abs(len1 - len2);
// 长链表先移动差值步
while (n != 0) {
n--;
cur1 = cur1.next;
}
// 两个链表同时移动
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else {
// 如果初始化为loop1,则下面的while循环无法执行
cur1 = loop1.next;
while (cur1 != loop1) {
// cur1在沿环移动的时候如果遇到loop2,则返回loop1
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
// cur1在沿环移动的时候如果一直没遇到loop2,说明两个链表为4.1,返回null
return null;
}
}
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node loop1 = getLoopNode2(head1);
Node loop2 = getLoopNode2(head2);
// 两个链表都不存在环
if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
}
// 两个链表都存在环
if (loop1 != null && loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
}
// 一个存在环,一个不存在环
return null;
}
}