本文共 621 字,大约阅读时间需要 2 分钟。
题目分析见
class Solution: # @param head, a ListNode # @return a list node def detectCycle(self, head): if None == head or None == head.next: return None pfast = head pslow = head #找第一次相遇的点,若存在环路,则肯定会相遇 while pfast and pfast.next: pfast = pfast.next.next pslow = pslow.next if pslow == pfast: break if pslow != pfast: return None #pfast从头开始,则下次相遇的点就是循环开始的点 pfast = head while True: if pfast == pslow: return pfast pfast = pfast.next pslow = pslow.next
转载地址:http://pumji.baihongyu.com/