文章目录
题目介绍
题解
结论 :若有环,则快慢指针相遇时慢指针还没有走完一圈。
java
class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
while (slow != head) {
slow = slow.next;
head = head.next;
}
return slow;
}
}
return null;
}
}