# 142. Linked List Cycle II

[https://leetcode.com/problems/linked-list-cycle-ii/](https://leetcode.com/problems/linked-list-cycle-ii/)

## solution

* 链表的while循环条件

```python
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None or head.next is None:
            return None

        slow = head
        fast = head
        while fast is not None and fast.next is not None:  # 注意这里的条件，先要确保有fast，才能确保有fast next
            slow = slow.next  # 曾经在这里判断了二者不等才移动，但其实起点时二者也相等
            fast = fast.next.next

            if slow == fast:
                fast = head

                while slow != fast:
                    slow = slow.next
                    fast = fast.next
                return slow
        return None
```

时间复杂度：O(n)\
空间复杂度：O(1)
