题解 | 链表中环的入口结点
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def EntryNodeOfLoop(self, pHead): # write code here fast = pHead slow = pHead while True: if fast.next and fast.next.next: fast = fast.next.next else: return None slow = slow.next if fast == slow: break fast = pHead # 这里用 slow = pHead 也行 while fast != slow: fast = fast.next slow = slow.next return slow
神奇的题目,第21行用fast或者slow都行