题目描述:

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. iOS第三方支付-支付宝支付

    处理手机中有无支付宝的情况支付宝调用接口: - (void)payOrder:(NSString *)orderStr      fromScheme:(NSString *)schemeStr    ...

  2. MetInfo安装

    安装MetInfo企业网站管理系统需要经历三个步骤:安装准备.上传文件.安装系统. 第一步:安装前的准备 环境要求:需要支持PHP并提供Mysql数据库的空间(虚拟主机),详细要求: 下载MetInf ...

  3. C# JSON字符串序列化与反序列化

    JSON与c#对象转换http://hi.baidu.com/donick/item/4d741338870c91fe97f88d33 C# JSON字符串序列化与反序列化 – http://www. ...

  4. UVA 11481 - Arrange the Numbers 数学

    Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You ca ...

  5. 如何搭建配置php开发环境

    PHP的配置 1.打开解压后的C:\Program Files (x86)\php-5.3.5文件夹 1 将php.ini-development文件并更改名称为php.ini(留个备份,好习惯) 2 ...

  6. Java-J2SE学习笔记-树状展现文件结构

    1.利用java.io相关类树状展现文件结构 2.判定给定路径是否为dir,是则递归,每一递归一层缩进一次 3.代码 package Test; import java.io.File; public ...

  7. jenkins集成自动化部署插件(一) deploy-plugin

    在实际情况中项目构建成功,特别是web项目构建成功是需要将war放到对应的服务上面,进行运行(测试的阶段可能就是发布到测试服务器上面)这样只需要指定构建的触发策略就可以自动构建以及部署,省去不少人工的 ...

  8. CentOS下对Apache的中文乱码处理

    # vi /etc/sysconfig/i18nLANG="en_US.UTF-8"SYSFONT="latarcyrheb-sun16" 默认的语言是英文,如 ...

  9. post提交/文件上传服务器修改

    第一步:修改在php5下POST文件大小的限制   1.编修php.ini   找到:max_execution_time = 30 ,这个是每个脚本运行的最长时间,单位秒,修改为: max_exec ...

  10. Android处理Bitmap的一些方法

    http://www.it165.net/pro/html/201305/5795.html # 文件与Bitmap间的方法 1. 从文件载入Bitmap 01./** 02.* @brief 从文件 ...