剑指offer面试题【52】----两个链表的第一个公共节点

    xiaoxiao2025-04-25  14

    题目描述

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

    代码实现

    # -*- 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

     

    最新回复(0)