Interview-code-practice-python
Interview-code-practice-python copied to clipboard
剑指offer中倒数第三题出现的问题:程序可以运行,但在牛客网上报错 'NoneType' object has no attribute 'next' 不知道为什么??
def MeetingNode(self, pHead):
if not pHead:
return None
pSlow = pHead.next #定义一个慢指针2
pFast = pSlow.next#定义一个快指针3
while pSlow != pFast : #判断快慢指针是否相等
pSlow = pSlow.next
pFast=pFast.next.next
if pFast==None:
break
return pSlow,pFast
当链表中没有环时,pFast.next.next最后会变成None,但是代码中没有判断pFast.next.next是否为空的条件,因此我在代码中加入了判断条件 if pFast==None: break