引入

快慢指针经常用于链表(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. oracle系列(二)用户管理

    SQL> conn /as sysdbaConnected to Oracle Database 11g Express Edition Release 11.2.0.2.0 Connected ...

  2. Angular.js-2入门

    1.angular与MVC MVC即Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界 ...

  3. Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡

    周末的时候一个正在学Linux的朋友问我,高可用怎么玩?我和他微信了将近三个小时,把Nginx和haproxy双主高可用教给他了,今天突然想把这个给写进博客里,供给那些正在学习Linux系统的朋友们, ...

  4. 「PHP」抽象工厂模式

    引言   所属:创建型模式,常用设计模式之一 参考资料: <大话设计模式>程杰   模式概述    官方定义:抽象工厂模式(Abstract Factory),提供一个创建一系列相关或互相 ...

  5. python三大器之while,if,for循环

    一.for循环(遍历循环) 在Python你可能要经常遍历列表的所有元素,对每个元素执行相同的操作;对于包含数字的列表,可能要对每个元素进行相同的计算;在网站中,可能需要显示文章中的每个标题等等.某一 ...

  6. 回顾爬虫的时候的一些小TIPS

    1 json.dumps的时候默认会用ascii 所以在写入文件的时候会需要用到的指令变为json.dumps(a,ensuer_ascii=False),这样将禁止转换为ascii 然后再写入的时候 ...

  7. linux 网络编程 3---(io多路复用,tcp并发)

    1,io模型: 阻塞io.非阻塞io.io多路复用,信号驱动io. 阻塞Io与非阻塞io的转换,可用fcntl()函数 #include<unistd.h> #include<fcn ...

  8. jquery.validate验证,jquery.Form插件提交,主要可以异步提交文件

    <script type="text/javascript"> $(function () { $form = $("#manuForm"); $b ...

  9. logger 配置文件详解

    Logback配置文件详解 Logback,Java 日志框架. Logback 如何加载配置的 logback 首先会查找 logback.groovy 文件 当没有找到,继续试着查找 logbac ...

  10. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...