leetcode python3 简单题141. Linked List Cycle
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第一百四十一题
(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.
中文:
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/linked-list-cycle
(2)解法
① 一个一个地试探
(耗时:1116ms,内存:16.6M)
class Solution:def hasCycle(self, head: ListNode) -> bool:if not head:return headm = []while head:if head in m:return Truem.append(head)head = head.nextreturn False
② 双指针
(耗时:76ms,内存:16.8M)
class Solution:def hasCycle(self, head: ListNode) -> bool:if not head:return headslow = headquick = headwhile quick and slow:slow = slow.nextif quick.next:quick = quick.next.nextelse:return Falseif quick is slow:return Truereturn False
leetcode python3 简单题141. Linked List Cycle相关推荐
- leetcode python3 简单题234. Palindrome Linked List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十四题 (1)题目 英文: Given a singly linked li ...
- leetcode python3 简单题206. Reverse Linked List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零六题 (1)题目 英文: Reverse a singly linked l ...
- leetcode python3 简单题203. Remove Linked List Elements
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零三题 (1)题目 英文: Remove all elements from ...
- leetcode python3 简单题160. Intersection of Two Linked Lists
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...
- leetcode python3 简单题21. Merge Two Sorted Lists(Linked)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十一题 (1)题目 英文: Merge two sorted linked li ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题202. Happy Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零二题 (1)题目 英文: Write an algorithm to det ...
- leetcode python3 简单题83. Remove Duplicates from Sorted List
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十三题 (1)题目 英文: Given a sorted linked list ...
- leetcode python3 简单题70. Climbing Stairs
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第七十题 (1)题目 英文: You are climbing a stair ca ...
最新文章
- Linux的常用命令!
- Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs
- 一对多和多对一的关系,用mybatis写
- CSS魔法堂:更丰富的前端动效by CSS Animation
- oracle数据库相关知识,Oracle数据库相关知识点复习
- 广告贴:本周六的SharePoint技术交流会
- JavaScript -- DOM树
- cad lisp 示坡线_AutoCAD命令-画示坡线
- AdapterView 及其子类
- sony 播放器 android,WALKMAN × 安卓9.0,索尼 NW-ZX505 播放器体验
- Twitter是什么软件?哪个国家的?怎么使用?免费吗?
- [TCP/IP] 基础知识总结
- Idea中怎么运行从github上下载的项目
- Type c手机怎么实现一边充电一边听歌(边充边听放方案)
- 神经网络ppt不足之处怎么写,神经网络ppt免费下载
- nodejs 实现二维码生成及图片合成
- 【STM32】标准库与HAL库对照学习教程外设篇--红外避障传感器
- python 检测行为 网络摄像头_使用Python扫描开放的网络摄像头
- 网安--第七章 恶意代码分析与防治
- 谢宾斯基三角形的几种生成方法