数据结构与算法 -- 返回两个链表的相交的第一个节点

总体思路

  • 1.首先是判断两个链表是否有环,有的话返回其环的入口 没有返回null
  • 2.一共两种情况 (1)两个无环链表相交(2)两个有环链表相交 (至于为啥没有一个有环一个没环的相交 因为我们的是单链表)
    • 其中两个有环链表相交又分为三种情况:
      在这里插入图片描述

先总结下涉及到的小知识点:

(1)求单链表是否有环 如果有环,返回环入口的第一个节点否则返回null

  • 细节思路:利用快慢指针做,快的一次两步慢的一次一步,快慢相交后,快从头指针开始走,一次改为一步,快慢同时开始走,相遇的就是环的第一个节点(你也可以用哈希表,就是费空间)

(2)求两个无环单链表是否相交,相交返回第一个相交的节点否则返回null

  • 细节思路:先求两个链表大小的差值,长的那个先走到跟短的一样长后,开始一起走,相遇到就是返回,到头了返回null

(3)两个有环单链表是否相交,相交返回第一个相交的几节点,否则返回null

  • 细节思路:见图 对于两个链表的第一个入环节点相同的情况(其实比较的是第一个入环节点的内存地址是否相同)直接转化为求两个无环链表第一个相交的节点的情况

  • 细节思路:对于两个链表的第一个入环节点不相同的情况,则从第一个链表的入环节点的下一个节点开始走,如果碰到了第二个链表的入环节点,那么,返回第一个链表或者第二个链表的入环节点均可,因为都是第一个,只是跟原先的链表的距离不同而已。如果走完没找到 那么就是图中的第一种情况了,直接返回null。

  • 首先 让我们看一下实现总体功能的函数(就是我们直接调用的函数):

public static Node getIntersectNode(Node head1, Node head2){
        // 1.首先是判断两个链表是否有环,有的话返回其环的入口 没有返回null
        // 2.一共两种情况 (1)两个无环链表相交(2)两个有环链表相交
        // 至于为啥没有一个有环一个没环的 相交 因为我们的是单链表
        if(head1 == null || head2 == null){
            return null;
        }
        //1.先求两个节点的入环节点
        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 slow = head.next;
        Node first = head.next.next;
        while (first != slow){
            if(first.next == null || first.next.next == null){
                return null;
            }
            first = first.next.next;
            slow = slow.next;
        }
        first = head;
        while (first != slow){
            first = first.next;
            slow = slow.next;
        }
        return first;
    }
  • 下面是求两个无环单链表是否相交的代码:
//这是求 两个无环链表相交的第一个节点
    public static Node noLoop(Node head1, Node head2){
        int len1 = 0;
        int len2 = 0;
        Node cur1 = head1;
        Node cur2 = head2;
        while (head1 != null){
            len1++;
            head1 = head1.next;
        }
        while (head2 != null){
            len2++;
            head2 = head2.next;
        }
        int n = len1 - len2;
        if(n > 0){
            while (n > 0){
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != null){
                if(cur1 == cur2){
                    return cur1;
                }
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return null;
        }else if(n < 0){
            n = Math.abs(n);
            while (n > 0){
                n--;
                cur2 = cur2.next;
            }
            while (cur1 != null){
                if(cur1 == cur2){
                    return cur1;
                }
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return null;
        }else{
            while (cur1 != null){
                if(cur1 == cur2){
                    return cur1;
                }
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return null;
        }
    }
  • 下面是求有环单链表是否相交的代码:
 //(2)两个有环链表相交 一共三种情况:
    // 1.无交点
    // 2.两个环的交点是一个
    // 3.两个环的交点不是一个
    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2){
        Node cur1 = null;
        Node cur2 = null;
        int n1 = 0;
        int n2 = 0;

        //第2种情况
        if(loop1 == loop2){
            cur1 = head1;
            cur2 = head2;
            while (cur1 != loop1){
                n1++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2){
                n2++;
                cur2 = cur2.next;
            }
            int n3 = n1 - n2;
            cur1 = head1;
            cur2 = head2;
            if(n3 > 0){
                while (n3 > 0){
                    n3--;
                    cur1 = cur1.next;
                }
                while (cur1 != loop1){
                    if(cur1 == cur2){
                        return cur1;
                    }
                    cur1 = cur1.next;
                    cur2 = cur2.next;
                }
                return cur1;
            }else if(n3 < 0){
                n3 = Math.abs(n3);
                while (n3 > 0){
                    n3--;
                    cur2 = cur2.next;
                }
                while (cur1 != loop2){
                    if(cur1 == cur2){
                        return cur1;
                    }
                    cur1 = cur1.next;
                    cur2 = cur2.next;
                }
                return cur1;
            }else {
                while (cur1 != loop1){
                    if(cur1 == cur2){
                        return cur1;
                    }
                    cur1 = cur1.next;
                    cur2 = cur2.next;
                }
                return cur1;
            }
        } else{
            Node p = loop1.next;
            while (p != loop1){
                if(p == loop2){
                    return loop1;
                }
                p = p.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);

        System.out.println(noLoop(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);

    }
全部评论

相关推荐

11-07 03:09
深圳大学 C++
实习秋招做的很差,也想总结一下自己的大学生涯吧。不算太摆,但是很迷。0.大学前高考发挥超常,才来到深大计软。大学前暑期基本上都是玩游戏了。接触了python(李笑来)但是没接触到online&nbsp;judge,也没去多了解编程生态、计算机行业。背了背单词,但是没去规划指标如六级,没制定计划不了了之。1.大一军训时去了校ACM培训,当时dev编译器都不会下载。军训期间积极看B站大学c语言课程。力扣,牛客都是知道的,但是没有成为很好的跳板。第二次培训,看不懂cpp的&nbsp;cin&amp;gt;&amp;gt;,网上搜了也没搞懂,再加上周末跟训得三个多小时,感觉跟不上放弃了。自费报了蓝桥杯,混了省二跟着一些机构课程学习,走的cpp路线。暑假在linux上熟悉vim操作。2.大二朝花夕拾,又去参加ACM训练,跟了一年,寒假都在码&nbsp;带懒标记的线段树。codeforce和力扣赛都在打打(竞赛还是有趣的)。集训队入队周赛打四场,校赛拿金,面试时表现差,说自己想就业,遂挂。当时四月多,2024华为软件精英挑战赛也在打,拿了80名(前64才有三等奖)。蓝桥杯国二。很多晚上跑步来消磨时间。3.大三上修了深大最强的计算机图形学,408找实习,投简历了说自己只有周末有空,遂没在找。也没看牛客真实行情。寒假随便做了个日志器,属于混过去了。当时接到字节的面试(人生处女面),前一天觉都睡不好,很紧张,手撕做的不好,话都说不利索了。面评脏。大三下找实习,cpp选手,没有很好经历、项目,运气好去了学校附近中厂实习。4.大四现在,貌似对开发不上心?没有好的offer(甚至hot100不会做)其实同届好多同学都拿的不错。还有保研C9的。嗯,考研吧。————对自己行为的分析:a.应试教育+应试家庭教育,我的个性是固执、遵规守矩的。b.还有莫名的孤独,明明有很多朋友,但还是没有很好的内驱力,没有坚定的理想。c.自己没有很好的调研、探索和规划能力。大家也可以锐评一下😊
_Matrice_:差不多的性格,不然不会本科时硬杠cpp(那个时候还没有大模型,啃完一整本primer和习题,还是做不出来什么东西),还找不到方向,相比之下学习一些应用层的同学已经能够参考别人的方法做出实用的应用了。学东西,找实习,感觉更多地是出于和别人比较,而不是自我内驱。不过...正如deft所说,人生不需要他人的建议,所以也没有标准化的路径,在能够自食其力的背景下慢慢找到自己的生活方式吧...。另外面试很多时候看运气、眼缘
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务