> For the complete documentation index, see [llms.txt](https://longxingtan.gitbook.io/mle-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://longxingtan.gitbook.io/mle-interview/01_leetcode/02_linked_list/142.-linked-list-cycle-ii.md).

# 142. 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)
