24 day4 两两交换链表中的节点删除链表倒数第n个节点(19)环形链表(1( 二 )


也就是说slow一定没有走到环入口3,而fast已经到环入口3了 。
这说明什么呢?
在slow开始走的那一环已经和fast相遇了 。
那有同学又说了,为什么fast不能跳过去呢? 在刚刚已经说过一次了,fast相对于slow是一次移动一个节点,所以不可能跳过去 。
public class Solution {public ListNode detectCycle(ListNode head) {ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;if (slow == fast) {// 有环ListNode index1 = fast;ListNode index2 = head;// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口while (index1 != index2) {index1 = index1.next;index2 = index2.next;}return index1;}}return null;}}
【24day4 两两交换链表中的节点删除链表倒数第n个节点(19)环形链表(1】注:文章中的图片来源于代码随想录,可以去他们的网站看看 。