题目

Given a linked list, determine if it has a cycle in it.

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

代码:oj在线测试通过 Runtime: 416 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
if head is None or head.next is None:
return False p1 = ListNode(0)
p1.next = head
p2 = ListNode(0)
p2.next = head result = False
while p1 is not None and p2 is not None:
if p1 == p2:
result = True
break
else:
p1 = p1.next
p2 = p2.next
if p2 is not None:
p2 = p2.next return result

思路

这是一道经典的题 关键点是快慢指针

p1是慢指针,一次走一步;p2是快指针,一次走两步;如果有循环,则快慢指针一定会在某一时刻遇上。

有个问题比较关键:为啥进入循环后,快指针一定能在某一时刻跟慢指针踩在同一个点儿上呢?

小白觉得可以如下解释:

假设现在快慢指针都在循环当中了,由于循环是个圈,则可以做如下的等价:“慢指针一次走一步,快指针一次走两步” 等价于 “慢指针原地不动,快指针一次走一步”这个其实跟物理学中的相对运动原理差不多。

欢迎高手来拍砖指导。

leetcode 【 Linked List Cycle 】 python 实现的更多相关文章

  1. leetcode Linked List Cycle python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  2. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  4. [LeetCode] Linked List Cycle 单链表中的环

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

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

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

  7. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  8. LeetCode: Linked List Cycle 解题报告

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

  9. LeetCode Linked List Cycle 解答程序

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

  10. [Leetcode] Linked list cycle ii 判断链表是否有环

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

随机推荐

  1. sublim插件(待续)

    imesupport SublimeText3默认不支持输入法跟随光标,这在输入中文的时候看起来不方便. 进入SublimeText3在上面菜单栏里Perferences点击PackageContro ...

  2. 搭建FTP服务

    (一)FTP服务概述 FTP服务概述:名称.功能.特点.端口 VSFTP:very secure FTP  端口:21 服务安装#yum install vsftpd lftp -y   ##lftp ...

  3. 深入浅出Nginx

    深入浅出Nginx   文章源自zfz_linux_boy   前言 Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 上图基 ...

  4. How To Secure Nginx with Let's Encrypt on Ubuntu 14.04

    https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14 ...

  5. 2018.7.22 Jdom与dom的区别

    SAX 优点:①无需将整个文档加载到内存,因而内存消耗少 ②推模型允许注册多个ContentHandler 缺点:①没有内置的文档导航支持 ②不能够随机访问XML文档 ③不支持在原地修改XML ④不支 ...

  6. maven解析xml+测试test+注解

    条件:maven项目 测试图: 创建maven项目,在maven项目中scr目录下有main.test(没有就创建) 一.解析XML文件方式 在main目录下有java.resources.webap ...

  7. iOS内存管理部分内容

    Objective-C 高级编程 iOS与OS X多线程和内存管理第一章部分讲述了关于ARC的内容,还讲述了关于修饰符的问题,还讲了好多底层的实现的内容,这些底层实现却往往是在面试的过程中经常被遇到的 ...

  8. java XML 通过BeanUtils的population为对象赋值 根据用户选择进行dom4j解析

    根据xml文件设计Student对象 <?xml version="1.0" encoding="UTF-8"?> <students> ...

  9. 开发必看 | iOS开发常用设计模式!

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~(一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则: ...

  10. selenium学习总结

    selenium主要用来做web自动化,分1.0和2.0两个版本,1.0包括selenium IDE.selenium Grid.selenium Remote Control,2.0在1.0的基础上 ...