142. Linked List Cycle II
题目
原始地址:https://leetcode.com/problems/linked-list-cycle-ii/#/description

/**
* 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) {
}
}
描述
给定一个链表,如果它有环返回环的第一个节点,否则返回null。
分析
本题是题目141的进阶版,除了判断链表有没有环,还要求出环的起始节点。

我们依旧使用快慢指针的方法来求解,还要增加一些定量的计算。设环的第一个节点(也就是需要求的节点)为q,fast和slow相遇的节点为p,按照图中标识,a为head到q的距离,b为q到p的距离,c为p到q的距离。环的长度为n,那么有:2 * (a + b) = a + b + c +b + xn,其中x为未知正整数。当然由于情况比较多,我们需要考虑一些特殊场景:
- 如果head在环内,那么会出现a=0的情况;
- 如果a=kn(k为正整数),那么b=0;
- 如果a<n,那么x为0,否则x大于0;
考虑到以上情况都满足,那么我们可以将原式简化为 a = c + xn。
当fast和slow相遇时,这两个指针都停在了p点,此时让slow2从head出发,同时slow继续前进,那么slow和slow2最终会在q点相遇。如果x>0,那么slow指针其实还会围绕环走x圈,当然最终还是会在q点和slow2相遇。
解法
/**
* 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, slow2 = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) {
while (slow != slow2) {
slow = slow.next;
slow2 = slow2.next;
}
return slow;
}
}
return null;
}
}

浙公网安备 33010602011771号