思路:
// @Title: 环形链表 II (Linked List Cycle II)
// @Author: qisiii
// @Date: 2024-09-04 17:10:23
// @Runtime: 0 ms
// @Memory: 43.6 MB
// @comment:
// @flag:
/**
* 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) {
ListNode slow = head,fast = head;
while(slow!=null&&fast!=null){
slow=slow.next;
fast=fast.next;
if(fast!=null){
fast=fast.next;
if(slow==fast){
fast=head;
while(slow!=fast){
fast=fast.next;
slow=slow.next;
}
return fast;
}
}
}
return null;
}
}