【题目】
在本题中,单链表可能有环,也可能无环。给定两个单链表的头节点head1和head2,这两个链表可能相交,也可能不相交。请实现一个函数, 如果两个链表相交,请返回相交的第一个节点;如果不相交,返回null即可。
要求:如果链表1的长度为N,链表2的长度为M,时间复杂度请达到0(N+M),额外空间复杂度请达到0 (1)。
有环的有两种解法
第一种用set或者map
public static ListNode EntryNodeOfLoopBySet(ListNode pHead)
{
if(pHead==null||pHead.next==null) return null;
Set<ListNode> set = new HashSet<>();
while (pHead!=null){
if (set.contains(pHead)) return pHead;
set.add(pHead);
pHead=pHead.next;
}
return 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;
}
无环的两种解法
map方法:遍历链表1存入节点到map,在遍历链表2,如果map有该值那就说交点,很简单不用多说
非map:两个链表分别求出长度len1,len2,然后找到最后两个结束时的节点,last1和last2.如果两者相等一定相交![在这里插入图片描述](https://img-blog.csdnimg.cn/20200305150041425.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjA2ODAw,size_16,color_FFFFFF,t_70)
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next; //cur1到了最后
}
while (cur2 != loop2) {
n--;
cur2 = cur2.next;//cur2到了最后
}
if (cur1 != cur2) { //如果两者的最后一个节点都不相等,返回null
return null;
}
cur1 = n > 0 ? head1 : head2; //通过n这个差值来定位长的链表是谁
cur2 = cur1 == head1 ? head2 : head1; //通过cur1长的旋转来判断cur2短的选择
n = Math.abs(n);
while (n != 0) { //让长的先走n步
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) { //两个一起走
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;//相遇的时候返回变量
}
两个都有环的解法
有三种情况:第一个头:head1,第一个环loop1.第二格一样
第一种:66型,不想交,loop1 != loop2
第二章:就是无环链表的尾巴多个环,按照无环链表判断即刻
第三种:两根鞭子型,两个链表交于一个圆loop1 == loop2
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) { //如果环相等对应图2 , 然后和上面无环一样
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next; //cur1到了最后
}
while (cur2 != loop2) {
n--;
cur2 = cur2.next;//cur2到了最后
}
cur1 = n > 0 ? head1 : head2; //通过n这个差值来定位长的链表是谁
cur2 = cur1 == head1 ? head2 : head1; //通过cur1长的旋转来判断cur2短的选择
n = Math.abs(n);
while (n != 0) { //让长的先走n步
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) { //两个一起走
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;//相遇的时候返回变量
} else { //否则判断是图1还是图3
cur1 = loop1.next;
while (cur1 != loop1) {//loop1跑
if (cur1 == loop2) { //如果跑的过程遇见loop2
return loop1; //那就返回
}
cur1 = cur1.next;
}
return null; //loop1跑完了还没找到,那就返回null
}
}