题解 | 两个链表的第一个公共结点

两个链表的第一个公共结点

https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def FindFirstCommonNode(self , pHead1 , pHead2 ):
        set_A = set()
        node1 = pHead1
        node2 = pHead2
        while node1:
            set_A.add(node1)
            node1 = node1.next
        while node2:
            if node2 in set_A:
                return node2
            node2 = node2.next
        return None



全部评论

相关推荐

03-12 12:33
嘉应学院 Python
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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