LeetCode160. 相交链表🌟🌟🌟🌟🌟简单

课后作业

问题描述

原文链接:160. 相交链表

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

图示两个链表在节点 c1 开始相交:

img

题目数据保证整个链式结构中不存在环。

注意,函数返回结果后,链表必须保持其原始结构。

自定义评测:

评测系统的输入如下(你设计的程序不适用此输入):

  • intersectVal – 相交的起始节点的值。如果不存在相交节点,这一值为 0
  • listA – 第一个链表
  • listB – 第二个链表
  • skipA – 在 listA 中(从头节点开始)跳到交叉节点的节点数
  • skipB – 在 listB 中(从头节点开始)跳到交叉节点的节点数

评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被视作正确答案 。

示例 1:

img

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
— 请注意相交节点的值不为 1,因为在链表 A 和链表 B 之中值为 1 的节点 (A 中第二个节点和 B 中第三个节点) 是不同的节点。换句话说,它们在内存中指向两个不同的位置,而链表 A 和链表 B 中值为 8 的节点 (A 中第三个节点,B 中第四个节点) 在内存中指向相同的位置。

示例 2:

img

输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

img

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示:

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 1 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listA 和 listB 没有交点,intersectVal 为 0
  • 如果 listA 和 listB 有交点,intersectVal == listA[skipA] == listB[skipB]

代码实现

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null){
            return null;
        }

        ListNode A = headA;
        ListNode B = headB;

        while(A != B){
            A = A == null ?headB : A.next;
            B = B == null ? headA : B.next;
        }

        return A;
    }
}

Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        if headA is None or headB is None:
            return None
        A = headA
        B = headB
        while A != B:
            A = A.next if A else headB
            B = B.next if B else headA
        return A

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if (headA == nullptr || headB == nullptr) {
            return nullptr;
        }

        ListNode *A = headA;
        ListNode *B = headB;

        while (A != B) {
            A = A == nullptr ? headB : A->next;
            B = B == nullptr ? headA : B->next;
        }

        return A;
    }
};

Go

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func getIntersectionNode(headA, headB *ListNode) *ListNode {
    if headA == nil || headB == nil {
        return nil
    }

    A := headA
    B := headB

    for A != B {
        A = A.Next
        B = B.Next

        if A == nil && B == nil {
            return nil
        }

        if A == nil {
            A = headB
        }

        if B == nil {
            B = headA
        }
    }

    return A
}

发表回复

后才能评论

评论(8)

  • 我来吃饭啦 永久会员 2024年6月18日 下午8:32

    打卡

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
            A, B = headA, headB
            while A != B:
                A = A.next if A else headB
                B = B.next if B else headA
            return A
    
  • Yuki 永久会员 2024年1月28日 上午11:37

    打卡

    “`
    class Solution {
    public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

        ListNode*curA=headA;
        ListNode*curB=headB;
        int lenA=0,lenB=0;
    
        while(curA!=NULL){
            lenA++;
            curA=curA->next;
        }
        while(curB!=NULL){
            lenB++;
            curB=curB->next;
        }
    
        curA=headA,curB=headB;
        if(lenB>lenA){
            swap(curA,curB);//curA指向更长的那条
            swap(lenA,lenB);
        }
        int gap=lenA-lenB;
        while(gap--){
            curA=curA->next;
        }
    
        while(curA!=NULL&&curB!=NULL){
            if(curA==curB){
                return curA;
            }
            curA=curA->next;
            curB=curB->next;
        }
    
        //跳出说明没交点
        return NULL;
    }
    

    };

  • uu 永久会员 2023年9月19日 下午7:34
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    
    // 以下两种时空复杂度一样
    // 时间复杂度:O(n)
    // 空间复杂度:O(1)
    
    // 【思路】消去差值
    // public class Solution {
    //     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            int lena = 0, lenb = 0;
            ListNode a = headA;
            ListNode b = headB;
            while(a != null) {
                lena++;
                a = a.next;
            }
            while(b != null) {
                lenb++;
                b = b.next;
            }
            int diff = Math.abs(lena - lenb);
            Boolean flag = lena > lenb ? true : false;
            a = headA;
            b = headB;
            while(a != null && b != null) {
                if(flag && diff > 0) {
                    a = a.next;
                    diff--;
                    continue;
                }else if(diff > 0) {
                    b = b.next;
                    diff--;
                    continue;
                }
                if(a == b) {
                    return  a;
                }
                else {
                    a = a.next;
                    b = b.next;
                }
            }
            return null;
        }
    }
    
    // 【代码优化】
    // public class Solution {
    //     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    //         if(headA == null || headB == null) {
    //             return null;
    //         }
    //         ListNode a = headA, b = headB;
    //         while(a != b) {
    //             a = a == null ? headB : a.next;
    //             b = b == null ? headA : b.next;
    //         }
    //         return a;
    //     }
    // }
    
  • 阿尚 普通 2023年8月17日 上午11:33
    //打卡
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode p1 = headA;
            ListNode p2 = headB;
    
            while(p1 != p2){
                if(p1 != null){
                    p1 = p1.next;
                }else{
                    p1 = headB;
                }
    
                if(p2 != null){
                    p2 = p2.next;
                }else{
                    p2 = headA;
                }
            }
    
            return p1;
        }
    }
    
  • 3_🔥 永久会员 2023年7月9日 下午10:29
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null){
                return null;
            }
    
            ListNode A = headA;
            ListNode B = headB;
    
            while(A != B){
                A = A == null ?headB : A.next;
                B = B == null ? headA : B.next;
            }
    
            return A;
        }
    }
    

    走路

  • 帅气的冬瓜 普通 2023年6月28日 下午2:30

    打卡

  • mpweixin用户 普通 2023年5月7日 下午8:45

    为什么A走到最后又走B 去了

  • mpweixin用户 普通 2023年4月12日 下午5:00

    如果 A 和 B 相交,那么 A 和 B 走过的路程是相同的,都是从各自的头节点到相交节点的距离加上从相交节点到相交部分尾部的距离。A 和 B 最终一定会同时到达相交节点。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理