题目

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. SAP CRM和C4C数据同步的两种方式概述:SAP PI和HCI

    SAP Cloud for Customer(C4C)和SAP其他传统产品进行数据同步的方式,如下图所示,可以使用SAP Netweaver Process Integration或者SAP HANA ...

  2. Jenkins添加项目说明,增加项目描述

    背景:往往正常Jenkins上呈现的内容,太过简短,不易直观看了解项目是干嘛的,如下面的内容: 解决方案,使用插件,Extra Columns Plugin 安装成功后配置,需要结合自定义视图使用,新 ...

  3. python Unittest+excel+ddt数据驱动测试

    #!user/bin/env python # coding=utf- # @Author : Dang # @Time : // : # @Email : @qq.com # @File : # @ ...

  4. 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术

    第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...

  5. while counter<10:

    [root@chenbj test]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat ...

  6. java获取文件后缀名(正则表达式)+文件名

    String fileName="asadas.jsp"; String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.png|.PNG)$" ...

  7. 【洛谷P1880】[NOI1995]石子合并

    石子合并 fmax[l][r]表示合并区间[l,r]的最大分值, fmin[l][r]表示合并区间[l,r]的最小分值 for(k l~r-1) fmax[l][r]=max(fmax[l][r],f ...

  8. 使用Jmeter性能测试,读取csv文件时的乱码问题

    读取csv参数乱码问题 发送请求时参数通过CSV文件读取,发送请求后显示错误,把获取的参数通过在线urlencode转码器转码后发现是乱码.打开csv设值,编码格式选择的是UTF-8,打开参数文件后发 ...

  9. gdb-pada调试实例

    先编写个简单的hello的程序 hello.c (ps:有没有头文件行不行,试试不就知道了) int main(){ printf("hello!\n"); int m,n; in ...

  10. Linux进程通信之匿名管道

    进程间的通信方式 进程间的通信方式包括,管道.共享内存.信号.信号量.消息队列.套接字. 进程间通信的目的 进程间通信的主要目的是:数据传输.数据共享.事件通知.资源共享.进程控制等. 进程间通信之管 ...