# Problem 142: Linked List Cycle II

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

## 思路

![](/files/-Lpv9w2vn71ujLiAv0VZ)

* 开始还是快慢指针的东西
* 发现有cycle之后（也就是说跳出了第一个while循环），head和slow同时出发，下次，head和slow\.next相遇的时候，就是cycle的位置
* **无他，记住就好了**

```java
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }

        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != slow) {
            if (fast == null || fast.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }

        while (head != slow.next) {
            head = head.next;
            slow = slow.next;
        }

        return head;
    }
}
```

## 易错点

1. 判断位置是看 head 和 slow\.next 的相遇点

   ```java
   while (head != slow.next) {
          head = head.next;
          slow = slow.next;
   }
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_4_linked_list/find-cycles/problem_142_linked_list_cycle_ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
