> 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/>

## 解法一：快慢指针

![](https://3747312504-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LXItOiQMDgh0S65YZXd%2F-LZck1QYW72pIlqz5mJS%2F-LZckQOvVpyO6fu36C3M%2FWechatIMG7.jpeg?alt=media\&token=a910d24b-4f45-4a92-ac31-61de87675b6b)

```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
```
