> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/1-200/142.-huan-xing-lian-biao-ii.md).

# 142. 环形链表 II

<https://leetcode-cn.com/problems/linked-list-cycle-ii/>

## 解法一：快慢指针

![](/files/-LZckQOvVpyO6fu36C3M)

```python
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """         
        hasCycle = False    #是否有环
        
        if not head or not head.next:  #边界
            return None
        #初始化
        slow = head
        fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:    #找到环即退出循环
                hasCycle = True
                break

        if hasCycle:    #若有环
            p = head  #新指针
            while p != slow:
                p = p.next
                slow = slow.next
            return p
        else:
            return None
```
