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

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

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 pHead1 == None or pHead2 == None:
            return None

        ph1 = pHead1
        ph2 = pHead2

        while ph1 != ph2:
            if ph1:
                ph1 = ph1.next
            else:
                ph1 = pHead2
            if ph2:
                ph2 = ph2.next
            else:
                ph2 = pHead1
        return ph1

全部评论

相关推荐

昨天 12:25
重庆大学 C++
到底要什么样的学历才配进呢
投递速腾聚创等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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