原题地址:http://oj.leetcode.com/problems/linked-list-cycle/

题意:判断链表中是否存在环路。

解题思路:快慢指针技巧,slow指针和fast指针开始同时指向头结点head,fast每次走两步,slow每次走一步。如果链表不存在环,那么fast或者fast.next会先到None。如果链表中存在环路,则由于fast指针移动的速度是slow指针移动速度的两倍,所以在进入环路以后,两个指针迟早会相遇,如果在某一时刻slow==fast,说明链表存在环路。

代码:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:# @param head, a ListNode# @return a booleandef hasCycle(self, head):if head == None or head.next == None:return Falseslow = fast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False

[leetcode]Linked List Cycle @ Python相关推荐

  1. leetcode: Linked List Cycle II

    http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...

  2. LeetCode Linked List Cycle II(floyd cycle)

    问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...

  3. leetcode - Linked List Cycle

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

  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 II

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

  6. [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 ...

  7. LeetCode Linked List Cycle II

    题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null 思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始 ...

  8. LeetCode Linked List Cycle

    题意:判断链表中是否有环 思路:从起点开始,一个每次移动一步,另一个每次移动两步,如果相遇,说明有环 代码如下: class Solution {public boolean hasCycle(Lis ...

  9. LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法

    题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...

  10. LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)

    Time:2019/4/7 Title: Linked List Cycle Difficulty: Easy Author:小鹿 题目:Linked List Cycle I Given a lin ...

最新文章

  1. “〜”(波浪号/波浪形/旋转)CSS选择器是什么意思?
  2. DL之DNN之BP:神经网络算法简介之BP算法/GD算法之不需要额外任何文字,只需要八张图讲清楚BP类神经网络的工作原理
  3. 创立一家互联网公司,需要几步?
  4. 你的 Docker 应用是安全的吗?
  5. c语言第4章作业,《C语言程序设第4章作业.doc
  6. 基于java ee的源码,基于java EE的云ERP系统
  7. 在python的解释器中使用函数_浅析Windows 嵌入python解释器的过程
  8. 用GDB调试PHP扩展
  9. Jenkins执行.bat 提示不是内部或外部命令
  10. layui文档通读笔记
  11. sap 服务采购订单研究
  12. CMPP3.0-超长短信
  13. java layoutinflater_LayoutInflater-使用
  14. 进入加密的QQ空间方法
  15. ThreadLocal的使用场景及使用方式
  16. 在cmd的命令下 用Mysql中建立一个表
  17. spreadjs学习笔记
  18. Android TV使用gsyVideoPlayer快进操作视频会回退
  19. EDK2编译报错,请帮我看看这个是什么错误
  20. redis哨兵模式出现connected_slaves:0解决办法

热门文章

  1. Android应用真机调试
  2. krb5安装包 linux_Linux:krb5
  3. Harmony OS — ToastDialog提示对话框
  4. Android Studio 报 非法字符: ‘\ufeff‘解决
  5. 手机 — oppo手机录音放在哪个文件夹里
  6. python基础 网络编程
  7. 微信小程序制作家庭记账本之一
  8. 洛谷4755 Beautiful Pair (分治)
  9. CCF 201604-4 游戏
  10. OC第七节——内存管理