资讯专栏INFORMATION COLUMN

LeetCode 之 JavaScript 解答第142题 —— 环形链表 II(Linked Li

whidy / 3075人阅读

摘要:说明不允许修改给定的链表。算法思路题目要求返回单链表中存在循环链表的位置。首先,先判断该单链表是否存在循环链表用两个快慢指针分别指向链表的头部,每次移动两步,每次移动一步,移动的步数是的两倍。

Time:2019/4/8
Title: Linked List Cycle II
Difficulty: medium
Author:小鹿

题目:Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

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

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos-1,则在该链表中没有环。

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

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

Follow up:
Can you solve it without using extra space?

Solve:
▉ 算法思路:
题目要求返回单链表中存在循环链表的位置。

1、首先,先判断该单链表是否存在循环链表?用两个快慢指针(fast、slow)分别指向链表的头部,fast 每次移动两步,slow 每次移动一步,fast 移动的步数是 slow 的两倍。

2、当 slow 与 fast 发生重合的时候,则存在链表。(slow 遍历完单链表一遍,fast 遍历了单链表两遍,2 倍的关系,如果 pos = 0 时,正好在头结点重合)。

3、如果 pos > 0 的时候。也就是说单链表中存在环,slow 到与 fast 指针重合的地方走过的路径为 n,fast 走过的路径是 slow 的两倍也就是 2n 。如果此时让 fast 每次走一步,走 n 步之后还会回到重合点。

4、那么怎么知道环接入的位置呢?这里稍微用的到点数学知识,看下图:

当 fast 指针和 slow 指针重合时,我们在声明一个 q 指针来计算到环结点的距离,因为 fast 改为每次移动一步,且 q 也要移动一步,fast 与 q 重合的地方就是环的接入点。(因为 slow 走过的路径和 fast 走过的路径相同,其中不重合的地方就是接入点)

▉ 代码实现:
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function(head) {
    //判断头结点是否为 null
    if(head == null || head.next == null){
        return null;
    }
    let fast = head;
    let slow = head;
    while(fast !== null && fast.next !== null){
        fast = fast.next.next;
        slow = slow.next;
        //查找接入点
        if(fast == slow){
            slow = head;
            while(slow !== fast){
                fast = fast.next;
                slow = slow.next;
            }
            return  slow;
        }
    }
    return null;
};

欢迎一起加入到 LeetCode 开源 Github 仓库,可以向 me 提交您其他语言的代码。在仓库上坚持和小伙伴们一起打卡,共同完善我们的开源小仓库!
Github:https://github.com/luxiangqia...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/103391.html

相关文章

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

    摘要:如果链表无环,则返回。说明不允许修改给定的链表。示例输入输出解释链表中有一个环,其尾部连接到第一个节点。两种方法哈希表哈希表添加节点时只要发现节点已经存在了,证明就有环形链表。 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该...

    hoohack 评论0 收藏0
  • LeetCode 142环形链表 II Linked List Cycle II

    摘要:如果链表无环,则返回。说明不允许修改给定的链表。示例输入输出解释链表中有一个环,其尾部连接到第一个节点。两种方法哈希表哈希表添加节点时只要发现节点已经存在了,证明就有环形链表。 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该...

    geekzhou 评论0 收藏0
  • LeetCode JavaScript 解答141 —— 环形链表 I(Linked Lis

    摘要:小鹿题目算法思路两种解题思路哈希表法遍历链表,没遍历一个节点就要在哈希表中判断是否存在该结点,如果存在,则为环否则,将该结点插入到哈希表中继续遍历。 Time:2019/4/7Title: Linked List CycleDifficulty: Easy Author:小鹿 题目:Linked List Cycle I Given a linked list, determine ...

    wangjuntytl 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0

发表评论

0条评论

whidy

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<