题目链接:
https://leetcode-cn.com/problems/linked-list-cycle-ii/
题目描述:
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
基本思路;
先使用快慢指针找到其交点,判断链表是带环的,在带环的情况下, 设定两个引用, 分别从链表头部和fast slow交点出发, 按照相同的速度同步往后走,当这两个引用指向同一对象时,此时这个对象就是环的入口。
代码实现:
/*
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
// 1. 使用快慢指针, 找到快慢指针的交点
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
// 此时链表就是带环的
break;
}
}
if (fast == null || fast.next == null) {
// 链表不带环
return null;
}
// 带环的情况, 设定两个引用, 分别从链表头部和fast slow交点出发, 按照相同的速度同步往后走
ListNode cur1 = head;
ListNode cur2 = fast;
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
// 这个位置就是环的入口
return cur1;
}
}