题目描述:

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

解题思路:

快的指针和慢的指针

代码如下:

# 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
"""
slow = fast = head while slow and fast and fast.next:
slow = slow.next
fast = fast.next.next if slow == fast:
return True return False

  

Python [Leetcode 141]Linked List Cycle的更多相关文章

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

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

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

  3. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

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

  5. 【leetcode❤python】141. Linked List Cycle

    #-*- coding: UTF-8 -*- #Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-link ...

  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. leetcode 141 Linked List Cycle Hash fast and slow pointer

    Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...

  8. 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 ...

  9. Java for 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 ex ...

随机推荐

  1. HDU 3998 Sequence (最长上升子序列+最大流)

    参考链接:http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html 题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子 ...

  2. jQuery实现表格隔行换色且感应鼠标高亮行变色

    jQuery插件实现表格隔行换色且感应鼠标高亮行变色 http://www.jb51.net/article/41568.htm jquery 操作DOM的基本用法分享http://www.jb51. ...

  3. 视频转换工具 Transmageddon

    点这里 Transmageddon 是一个采用 Python 语言开发的视频转换工具,支持输出几乎所有的视频格式,同时也可以生成指定平台下的视频格式. 软件界面如下图所示

  4. mysql 存储过程事务

    DECLARE t_error INTEGER DEFAULT ; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=; START TRAN ...

  5. Java IO(一)

       字节流的两个基类: InputStream OutputStream 字符流的两个基类: Reader Writer Writer 先学习一下字符流的特点. 既然IO流是用于操作数据的,那么数据 ...

  6. 李洪强iOS开发之苹果使用预览截图

    李洪强iOS开发之苹果使用预览截图 01 在预览的图片中选中你要截得区域  02 - command + C   03 - Command + N 04 - Command + S (保存)

  7. TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE

    TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE TASK_INTERRUPTIBLE 和TASK_UNINTERRUPTIBLE 的区别 TASK_INTERRUPT ...

  8. Linux Command Line 解析

    Linux Command Line 解析 0 处理模型 Linux kernel的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line进行配置的.在进行后续分析之前,先 ...

  9. 关于JLINK固件丢失或升级固件后提示Clone的解决办法

    本人用的JLINK仿真器(某宝上买的),在使用新版KEIL时,提示要升级固件,升级后就出现JLINK is Clone的提示.在网上找了许多关于修复的资料,都觉得不是很好.经过本人反复试验,总算找到比 ...

  10. css3:target页内跳转

    :target 用于选取当前活动的目标元素 <!DOCTYPE html> <html> <head lang="en"> <meta c ...