链表中环的入口节点

2019-12-22  本文已影响0人  而立之年的技术控
微信图片_20191222194317.jpg
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        if pHead is None or pHead.next is None:
            return None
        fast = pHead
        slow = pHead
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                break
        fast = pHead
        while fast != slow:
            fast = fast.next
            slow = slow.next
        return fast
上一篇 下一篇

猜你喜欢

热点阅读