剑指offer-两个链表的第一个公共结点-链表-python

题目描述

输入两个链表,找出它们的第一个公共结点。
 
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        lst1 = []
        lst2 = []
        result = []
 
        if not pHead1 or not pHead2:
            return None
 
        p1 = pHead1
        p2 = pHead2
 
        while p1:
            lst1.append(p1)
            p1 = p1.next
        while p2:
            lst2.append(p2)
            p2 = p2.next
 
        while lst1 and lst2:
            node1 = lst1.pop()
            node2 = lst2.pop()
            if node1 == node2:
                result.append(node1)
         
        if result:
            node = result.pop()
            return node

 

posted @ 2019-12-10 09:19  ayew  阅读(132)  评论(0编辑  收藏  举报