LeetCode 142. 环形链表 II Linked List Cycle II

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。

说明:不允许修改给定的链表。

进阶:
你是否可以使用 O(1) 空间解决此题?

![](https://img-blog.csdnimg.cn/img_convert/375dd30c1a72d14f6b5333c38611e62a.png#align=left&display=inline&height=171&margin=[object Object]&originHeight=171&originWidth=531&size=0&status=done&style=none&width=531)

输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。

![](https://img-blog.csdnimg.cn/img_convert/904e695806c5f324ba4ee957c709f268.png#align=left&display=inline&height=105&margin=[object Object]&originHeight=105&originWidth=201&size=0&status=done&style=none&width=201)

输入:head = [1,2], pos = 0
输出:返回索引为 0 的链表节点
解释:链表中有一个环,其尾部连接到第一个节点。

![](https://img-blog.csdnimg.cn/img_convert/303c7d41a5ccb0a9acdcc025bca299c1.png#align=left&display=inline&height=65&margin=[object Object]&originHeight=65&originWidth=65&size=0&status=done&style=none&width=65)

输入:head = [1], pos = -1
输出:返回 null
解释:链表中没有环。
哈希法
public ListNode detectCycle(ListNode head) {
    ListNode pos = head;
    Set<ListNode> visited = new HashSet<ListNode>();
    while (pos != null) {
        if (visited.contains(pos)) {
            return pos;
        } else {
            visited.add(pos);
        }
        pos = pos.next;
    }
    return null;
}

复杂度分析
时间复杂度:O(N),其中 N 为链表中节点的数目。我们恰好需要访问链表中的每一个节点。
空间复杂度:O(N),其中 N 为链表中节点的数目。我们需要将链表中的每个节点都保存在哈希表当中。

快慢双指针法

我们使用两个指针,fast 与 slow。它们起始都位于链表的头部。随后,slow 指针每次向后移动一个位置,而 fast 指针向后移动两个位置。如果链表中存在环,则 fast 指针最终将再次与 slow 指针在环中相遇。

设链表中环外部分的长度为 a。slow 指针进入环后,又走了 b 的距离与 fast 相遇。此时,fast 指针已经走完了环的 n 圈,
因此它走过的总距离为 a+n * (b+c) + b = a + (n+1)b + nc。

![](https://img-blog.csdnimg.cn/img_convert/735fffc3830f3c0a18cc5056c91d3143.png#align=left&display=inline&height=287&margin=[object Object]&originHeight=1125&originWidth=2000&size=0&status=done&style=none&width=510)

根据题意,任意时刻,fast 指针走过的距离都为 slow 指针的 2 倍。
因此,我们有 a + (n+1) * b + n * c = 2*(a+b) ⟹ a = c + (n−1) * (b+c)

有了 a = c + (n-1) * (b+c) 的等量关系,我们会发现:从相遇点到入环点的距离加上 n-1圈的环长,恰好等于从链表头部到入环点的距离。

因此,当发现 slow 与 fast 相遇时,我们再额外使用一个指针ptr。起始,它指向链表头部;随后,它和 slow 每次向后移动一个位置。最终,它们会在入环点相遇。

public ListNode detectCycle(ListNode head) {
    if (head == null) {
        return null;
    }
    ListNode slow = head, fast = head;
    while (fast != null) {
        slow = slow.next;
        if (fast.next != null) {
            fast = fast.next.next;
        } else {
            return null;
        }
        if (fast == slow) {
            ListNode ptr = head;
            while (ptr != slow) {
                ptr = ptr.next;
                slow = slow.next;
            }
            return ptr;
        }
    }
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值