python 剑指offer 面试题37

来源:互联网 发布:javascript var 对象 编辑:程序博客网 时间:2024/06/17 09:18
class ListNode:    def __init__(self, x):        self.val = x        self.next = Noneclass Solution:    def FindFirstCommonNode(self, pHead1, pHead2):        nLength1 = self.GetListLength(pHead1)        nLength2 = self.GetListLength(pHead2)        nLengthDiff = abs(nLength1 - nLength2)        if nLength1 > nLength2:            pListHeadLong = pHead1            pListHeadShort = pHead2        else:            pListHeadLong = pHead2            pListHeadShort = pHead1        for i in range(nLengthDiff):            pListHeadLong = pListHeadLong.next        while pListHeadLong != None and pListHeadShort != None and pListHeadLong != pListHeadShort:            pListHeadLong = pListHeadLong.next            pListHeadShort = pListHeadShort.next        pFirstCommonNode = pListHeadLong        return pFirstCommonNode    def GetListLength(self, pHead):        nLength = 0        while pHead != None:            pHead = pHead.next            nLength += 1        return nLength

原创粉丝点击