题目描述:

中文:

给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

英文:

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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

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

题目来源:力扣

力扣——Linked List Cycle(环形链表) python实现的更多相关文章

  1. 力扣 ——Linked List Cycle II(环形链表 II) python实现

    题目描述: 中文: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). ...

  2. LeetCode 141. Linked List Cycle环形链表 (C++)

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

  3. 力扣(LeetCode)环形链表 个人题解

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入: ...

  4. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

  5. 141 Linked List Cycle 环形链表

    给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...

  6. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  7. 力扣—Reorder List(重排链表)python实现

    题目描述: 中文: 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点 ...

  8. 力扣——Partition List(分隔链表) python实现

    题目描述: 中文: 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = ...

  9. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

随机推荐

  1. elasticsearch failed to load elasticsearch nodes

    转载:https://blog.csdn.net/weixin_44714808/article/details/90049315 elasticsearch 安装在linux时使用springboo ...

  2. golang接口

    接口是方法的集合,接口不需要考虑类型的属性是否一致,只需要考虑类型是否实现了接口的方法. 比如接口不需要考虑例二中的类型student和employee的属性,都可以传入接口,只需要他们实现了接口中的 ...

  3. Vue.js----router(路由)

    什么是路由? 路由可看做是一个键值对,K-V. K => path()路径地址 V=> 处理请求的回调函数 前台路由 K => path()路径地址 V=> 路由组件 编写使用 ...

  4. 【leetcode】823. Binary Trees With Factors

    题目如下: Given an array of unique integers, each integer is strictly greater than 1. We make a binary t ...

  5. LDD3 第13章 USB驱动程序

    通用串行总线(USB)是主机和外围设备之间的一种连接.最新USB规范修订增加了理论上高达480Mbps的高速连接. 从拓扑上看,USB子系统并不是以总线的方式来布置的,它是一颗由几个点对点的连接构建而 ...

  6. Codeforces 837D--Round Subset (DP)

    原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...

  7. QUartus 使用之001_空格_箭头切换----FPGA--001

    Quartus里的Tab键,按下后,显示的为什么是箭头,以前显示的是空白,图片如下: 解决方式如下: 修改后效果图如下:

  8. k-近邻算法(kNN)笔记

    #mat()函数可以将数组(array)转化为矩阵(matrix)# randMat = mat(random.rand(4,4))# 求逆矩阵:randMat.I# 存储逆矩阵:invRandMat ...

  9. [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)

    题目背景 $Billions\ of\ lighthouses...stuck\ at\ the\ far\ end\ of\ the\ sky.$ 题目描述 平面有$n$个灯塔,初始时两两之间可以相 ...

  10. [CSP-S模拟测试]:春思(数学)

    蝶恋花·春景花褪残红青杏小.燕子飞时,绿水人家绕.枝上柳绵吹又少.天涯何处无芳草!墙里秋千墙外道.墙外行人,墙里佳人笑.笑渐不闻声渐悄.多情却被无情恼.(本词是伤春之作,写春景清新秀丽.同时,景中又有 ...