Time:2019/4/8
Title: Linked List Cycle II
Difficulty: medium
Author:小鹿
javascript
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.java
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.node
Note: Do not modify the linked list.git
给定一个链表,返回链表开始入环的第一个节点。 若是链表无环,则返回 null
。github
为了表示给定链表中的环,咱们使用整数 pos
来表示链表尾链接到链表中的位置(索引从 0 开始)。 若是 pos
是 -1
,则在该链表中没有环。算法
说明:不容许修改给定的链表。编程
Example 1:bash
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:ui
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:this
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?
题目要求返回单链表中存在循环链表的位置。
一、首先,先判断该单链表是否存在循环链表?用两个快慢指针(fast、slow)分别指向链表的头部,fast 每次移动两步,slow 每次移动一步,fast 移动的步数是 slow 的两倍。
二、当 slow 与 fast 发生重合的时候,则存在链表。(slow 遍历完单链表一遍,fast 遍历了单链表两遍,2 倍的关系,若是 pos = 0 时,正好在头结点重合)。
三、若是 pos > 0 的时候。也就是说单链表中存在环,slow 到与 fast 指针重合的地方走过的路径为 n,fast 走过的路径是 slow 的两倍也就是 2n 。若是此时让 fast 每次走一步,走 n 步以后还会回到重合点。
四、那么怎么知道环接入的位置呢?这里稍微用的到点数学知识,看下图:
当 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/luxiangqiang/JS-LeetCode 欢迎关注我我的公众号:「一个不甘平凡的码农」,记录了本身一路自学编程的故事。