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

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

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

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

#
# 
# @param pHead1 ListNode类 
# @param pHead2 ListNode类 
# @return ListNode类
#
class Solution:
    def FindFirstCommonNode(self , pHead1 , pHead2 ):
        # write code here
        if not pHead1 or not pHead2:
            return None

        p1 = []
        p2 = []
        res = None
        while pHead1:
            p1.append(pHead1)
            pHead1=pHead1.next

        while pHead2:
            p2.append(pHead2)
            pHead2=pHead2.next
        
        while (p1 and p2):
            tmp1 = p1.pop()
            tmp2 = p2.pop()
            if (tmp1 != tmp2):
                return res
            else:
                res = tmp1
            
        return res

Notes:

此题输出为第一个公共结点,不是结点的值。

ref:

https://blog.csdn.net/wszll_Alex/article/details/86648731

全部评论

相关推荐

09-09 16:12
已编辑
成都理工大学 Java
future0210:学java就是好啊,啥都能转
点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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