引入

快慢指针经常用于链表(linked list)中环(Cycle)相关的问题。LeetCode中对应题目分别是:

  1. 141. Linked List Cycle 判断linked list中是否有环
  2. 142. Linked List Cycle II 找到环的起始节点(entry node)位置。

简介

  1. 快指针(fast pointer)和慢指针(slow pointer)都从链表的head出发。
  2. slow pointer每次移动一格,而快指针每次移动两格。
  3. 如果快慢指针能相遇,则证明链表中有环;否则如果走到头了依然没有相遇,则没有环。

快慢指针的具体代码(C++, Python, Java版本)可以参考这个链接

问题详解 —— 为什么如果有环,则快慢指针必定会相遇?

我们假设以下变量:

\(L_1\):起始节点(head node)到环起始节点(entry node)的距离。

\(C\): 环的长度。

假设我们的慢指针移动了\(x\)步,那么快指针就移动了\(2x\)步。

那么必定有$$(x-L_1)% C= (2x-L_1) % C $$$$(2x-x)% C = 0$$$$x%C=0$$

以上三个式子步步可逆,由于\(C\)是给定的fixed value,而\(x\)每步都在上升,因此必定有一个\(x=wC(w\in {N})\)使得他们相遇。并且有\(L_1 \le x\)所以必有\(x=\)⌈\(\frac{L_1}{C}\)⌉\(C\)为他们第一次相遇的地点。因此有\(x< L_1 + C\) where \(x=\)⌈\(\frac{L_1}{C}\)⌉\(C\)

这也就意味着他们相遇的地方一定是慢指针在环里的第一圈。

问题详解 —— 如何找到环的起始节点?

我们再增加一些变量:

\(L_2\): 环起始节点(entry node)到快慢指针相遇节点的距离。

\(k\): 慢指针和快指针相遇的时候,慢指针走了的距离。

注意到,因为快指针走了的距离总是慢指针走了的距离的两倍,因此\(2k\)是慢指针和快指针相遇的时候,快指针走了的距离。

由慢指针可以得出$$L_1+L_2=k$$由快指针可以得出,其中n是快指针已经走过的圈数$$L_1+nC+L_2 = 2k$$结合上述两个式子,我们可以得出$$nC=k$$

当慢指针在遇到了快指针之后,慢指针又马上移动了,那么慢指针需要移动\(p\)步后就可以让慢指针就回到了环起始节点(entry node)。与此同时,在快慢指针相遇之后,又有一个指针马上从原点出发,那么它需要经过\(q\) 步才能到达起始节点(entry node)而且与慢指针相遇。

当慢指针和这个新指针相遇的时候,有$$(p-L_1)%C=(q+L_2)%C$$

离散数学中的定理可知$$(p-q-L_1-L_2)%C=0$$$$(p-q-nC)%C=(p-q)%C=0$$

不妨取\(p=q\)即\(p-q=0\Rightarrow (p-q)\%C=0\)

因此当他们同时回到环起始节点(entry node)的时候,有慢指针第一次相遇后走过的距离\(p\)和新指针走的距离\(p=q\)。

值得注意的是\(L_2< C\),因此,他们第一相遇的时候必有\(q<C-L_2\),也就是说慢指针和新指针第一次相遇的时候,他们必定都在环起始节点(entry node)。

LeetCode 对应代码

判断linked list中是否有环(141. Linked List Cycle)

这个算法的时间复杂度是O(n)

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None:
return False
slow = head
fast = head
while(fast.next and fast.next.next):
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

找到环的起始节点(entry node)位置(142. Linked List Cycle II)

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return None
slow, fast, new_node = head, head, head
while(fast.next and fast.next.next):
slow = slow.next
fast = fast.next.next
if slow == fast:
while slow != new_node:
new_node = new_node.next
slow = slow.next
return new_node
return None

【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现的更多相关文章

  1. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  2. Leetcode 141题 环形链表(Linked List Cycle) Java语言求解

    题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map ...

  3. linked-list-cycle (快慢指针判断是否有环)

    class Solution { public: bool hasCycle(ListNode *head) { if (head == NULL) return NULL; //空表 ListNod ...

  4. Linked List Cycle I&&II——快慢指针(II还没有完全理解)

    Linked List Cycle I Given a linked list, determine if it has a cycle in it. Follow up: Can you solve ...

  5. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  6. [leetcode]141. Linked List Cycle判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  7. reorder-list——链表、快慢指针、逆转链表、链表合并

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  8. sort-list——链表、快慢指针找中间、归并排序

    Sort a linked list in O(n log n) time using constant space complexity. 链表,快慢指针找中点,归并排序. 注意判断条件fast-& ...

  9. 深入理解C指针之三:指针和函数

    原文:深入理解C指针之三:指针和函数 理解函数和指针的结合使用,需要理解程序栈.大部分现代的块结构语言,比如C,都用到了程序栈来支持函数的运行.调用函数时,会创建函数的栈帧并将其推到程序栈上.函数返回 ...

随机推荐

  1. NLP语言模型

    语言模型: I. 基本思想 区别于其他大多数检索模型从查询到文档(即给定用户查询,如何找出相关的文档), 语言模型由文档到查询,即为每个文档建立不同的语言模型,判断由文档生成用户查 询的可能性有多大, ...

  2. js常用共同方法

    var uh_rdsp = (function(){ //获取根目录 var getContextPath = function(){ var pathName = document.location ...

  3. webuploader实现上传视频

    之前有人让我做一个webuploader上传视频,但是一直没有时间,现在抽出了时间来.来完成以下这个简单的demo 第一步,上传视频和上传 图片有什么区别么? 其实是没有的,因为执行的操作都是上传,所 ...

  4. CentOS中的 yum upgrade 和 yum update 的区别

    通过 man yum 的帮助信息了解 yum update 和 yum upgrade: update If run without any packages, update will update ...

  5. QueryableHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  6. 解决thinkphp query()执行原生SQL语句成功结果报错的问题

    1.query方法 query方法用于执行SQL查询操作,如果数据非法或者查询错误则返回false,否则返回查询结果数据集(同select方法). 2.execute方法 execute用于更新和写入 ...

  7. Python系列之入门篇——pytables及其客户端

    pytables及其客户端查看 pytables # ubuntu sudo apt-get install python-tables pip install flask flask-httpaut ...

  8. For-each Loop,Index++ Loop , Iterator 那个效率更高

    平时在写Java/C# 程序的时候,会写很多的Loop 语句,for() 及 Iterator loop 及Java 8 的foreach Loop, 这些Loop 那种效率最高呢?写个小程序测试一下 ...

  9. ruby 条件判断&循环控制

    参考:https://www.jb51.net/article/66709.htm

  10. vim 对齐线

    ** 从https://github.com/Yggdroot/indentLine下载 indentLine插件 git clone https://github.com/Yggdroot/inde ...