力扣-142.环形链表 II
题目链接
142.环形链表 II
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.next != null && fast.next.next != null && slow != fast) {fast = fast.next.next;slow = slow.next;}if (fast != slow) {return null;}slow = head;while (fast != slow) {fast = fast.next;slow = slow.next;}return slow;}
}
小结:1.快慢指针判断是否有环;2.双指针寻找入环点。详细推导在这里