剑指offer 36. 两个链表的第一个公共结点

原题

输入两个链表,找出它们的第一个公共结点。

Reference Answer

思路分析

将链表中节点放入list后进行操作,对这种确定公共节点,计算数目之类的问题无往不利。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        if not pHead1 or not pHead2:
            return None
        temp = []
        start1 = pHead1
        start2 = pHead2
        while start1:
            temp.append(start1)
            start1 = start1.next
        while start2:
            if start2 in temp:
                return start2
            start2 = start2.next
        return None
                            

猜你喜欢

转载自blog.csdn.net/Dby_freedom/article/details/84309830