劍指offer面試題【52】----兩個鏈表的第一個公共節點

題目描述

輸入兩個鏈表,找出它們的第一個公共結點。

代碼實現

# -*- 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
        l=[]
        while pHead1:
            l.append(pHead1)
            pHead1=pHead1.next
        while pHead2:
            if pHead2 in l:
                return pHead2
                break
            pHead2=pHead2.next

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章