Leetcode 142. 环形链表 II

注意的点:
1、注意里面的判断条件,虽然代码少但是其实逻辑很多。
解法:快慢指针
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:# 快慢指针slow, fast = head, headif not head: return Nonewhile fast.next and fast.next.next:slow = slow.nextfast = fast.next.nextif slow == fast: break # 得放在后面if not fast.next or not fast.next.next: return Noneslow = headwhile slow != fast:slow = slow.nextfast = fast.nextreturn fast