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

☆樱花仙子☆ 提交于 2019-12-10 09:36:10

题目描述

输入两个链表,找出它们的第一个公共结点。
 
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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!