美文网首页leetcode
160.相交链表

160.相交链表

作者: 胖胖的熊管家 | 来源:发表于2023-07-11 19:19 被阅读0次

    160.相交链表

    解题思路

    将两个链表连起来,这样用两个指针去找相同的。

    JavaScript解法代码

    var getIntersectionNode = function(headA, headB) {
        let p1 = headA, p2 = headB;
        while(p1 != p2){
            if(p1 == null){
                p1 = headB
            }
            else{
                p1 = p1.next
            }
            if(p2 == null){
                p2 = headA
            }
            else{
                p2 = p2.next
            }
        }
        return p1
    };
    

    相关文章

      网友评论

        本文标题:160.相交链表

        本文链接:https://www.haomeiwen.com/subject/zhluudtx.html