1. 程式人生 > >【劍指Offer】36兩個連結串列的第一個公共結點

【劍指Offer】36兩個連結串列的第一個公共結點

題目描述

輸入兩個連結串列,找出它們的第一個公共結點。

時間限制:1秒;空間限制:32768K;本題知識點:連結串列

解題思路

# -*- 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
        p1 = pHead1
        while p1!=None:
            p2 = pHead2
            while p2!=None:
                if p1 == p2: #公共節點不等於有相同值的節點
                    return p1
                p2 = p2.next
            p1 = p1.next
        return