在本题中,单链表可能有环,也可能无环。给定两个单链表的头节点 head1和head2,这两个链表可能相交,也可能不相交。请实现一个函数, 如果两个链表相交,请返回相交的第一个节点;如果不相交,返回null 即可。 要求:如果链表1的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外空间复杂度请达到O(1)
这是节点类
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
什么是单链表有环?
有环,末尾不会是null节点,而是连接到自己,一直转圈下去
无环,末尾是null节点
判断有环无环,可以使用set集合,当遇见重复地址的时候判断为有环,null为无环
//不使用set集合,设计两个指针,快和慢,快的走两步,慢的走一步,当快指针遇见null的时候为无环,当快指针遇见慢指针的时候,让快指针从头开始重新走,一步一步走,遇见慢指针的地方,为环节点
set集合方法过于简单
这是第二种方法
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;
}
当结算出两个节点有环还是无环后,可以分情况进行判断
2个有无环
他们如果相交那么必定是节点中有·同一个,然后他们只后的节点也会是同一个。
然而一个无环的和一个有环的,是不会出现相交节点的,因为如果相交了,是不可能出现一个有终点,一个没终点的
两个无环的链表,找相交节点,
如果最后的尾节点,不相等,可以判断为不相交
①使用set集合,先添加进一条链表再添加另一条链表,遇见重复的节点就确定为相交节点。
②计算出两条链表的分别总长度,让比较长的先走,长 - 短 长度 ,那么他们一定可以在相交处相遇。
两个有环的节点
他们相交的话,只会在这两个地方相交
第一种情况,两条链表在结环之前就相遇了,他们的环节点是相同的。
我们可以忽略这个环,把结环的地方当成结束的地方,计算两条链表的分别总长度,让长的先走 长 - 步 步 ,然后一起走,一定可以在相交的地方遇见(或者使用set集合,把结环之前的节点全部添加进去)
第二种情况
我们让一个环节点,走,只管走,如果能在转完一圈前遇见 另一个环节点,那么就确定了相交节点,(这两个环节点都可以当做相交节点)。如果转完了一圈,那么他们不相交
代码
public class Test17 {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 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);
}
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 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.next!=loop1){
++n;
cur1 = cur1.next;
}
while(cur2.next!=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 Node noLoop(Node head1,Node head2){
int n=0;
Node cur1=head1;
Node cur2=head2;
while(cur1.next!=null){
cur1=cur1.next;
++n;
}
while(cur2.next!=null){
cur2=cur2.next;
--n;
}
if(cur1!=cur2){//如果他们的最终终点不在一起说明他们从未连接过
return null;
}
//判断 head1 和 head2谁比较靠长 让比较长的链表先走 (长链表长度-短链表长度) 步
cur1 = n>0? head1:head2;
cur2 = n>0? head2:head1;
n = Math.abs(n);
while(n--!=0){
cur1= cur1.next;
}
//让他们碰到一起那个点就是交点
while(cur1!=cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
public static Node getLoopNode(Node head){
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node slow = head.next;
Node fast = head.next.next;
while(slow!=fast){
if(fast.next==null||fast.next.next==null){
return null;
}
fast = fast.next.next;
slow = slow.next;
}
fast=head;
while(fast!=slow){
slow = slow.next;
fast = fast.next;
}
return fast;
}
}