Python3解leetcode Linked List Cycle
问题描述:
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.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
思路1:
设置两个指针,一个每次走一步,一个每次走两步,那么如果有环,两个指针一定会相遇。
但这个代码写出来仅仅beats 28.6%,效率不高
代码1:
# 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
head2 = head
while head2.next != None and head.next != None:#当两个指针的next都不为None
if (head2.next.next != None):#如果走两步的指针head2的next.next不为None
head2 = head2.next.next
else:
return False
head = head.next
if(head == head2):
return True
return False
将上述思路优化后,代码如下:
if not head or not head.next: return False
head2 = head
while not head2 and not head2.next:#只需要关注走的比较快的指针是否为空即可,若较快指针不为空,则较慢指针肯定不为空
head2 = head2.next.next
head = head.next
if(head == head2):
return True
return False
思路2:
循环遍历整个列表,将遍历过的节点值设置为inf(最大值),如果后续遍历的节点值有等于inf的,则证明有环,否则就是没有环。
该算法写出来beats65.7%的人,还需要优化
代码2:
# 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
head.val = float('inf')
while head.next != None:#当指针的next都不为None
if head.val == head.next.val:
return True
else:
head = head.next
head.val = float('inf')
return False
Python3解leetcode Linked List Cycle的更多相关文章
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle 解题报告
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode Linked List Cycle 解答程序
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
随机推荐
- Java学习之路 第四篇 oop和class (面向对象和类)
本人水平有限,创作本文是为了记录学习和帮助初学者学习,欢迎指正和补充 一.面向对象编程的设计概述 很多同学都在学校学了电脑的编程,现在的书籍大部分都是oop面向对象编程,一个很抽象的的名字,比较难以理 ...
- excel表格定义导入到powerdesigner脚本
打开powerdesigner,shift + ctrl + X 打开脚本窗口 输入执行的脚本,点 run 即可. 简单的导入Excel脚本 '开始 Option Explicit Dim mdl ' ...
- Oracle学习第一篇—安装和简单语句
一 安装 10G ----不适合Win7 Visual Machine-++++Visual Hard Disk 先安装介质(VM)---便于删除 11G-----适合Win7 1 把win64_1 ...
- EF6&EFCore 注册/使用实体类的正确姿势
首先回顾下EF中常规使用流程 1.新建实体类以及实体配置(data annotation或fluent api) [Table("Users")] public class Use ...
- 【Scala】Scala的Predef对象
隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...
- python 基础 4.1 函数的参数
#/usr/bin/python #coding=utf-8 #@Time :2017/10/24 9:09 #@Auther :liuzhenchuan #@File :函数的参数.py # ...
- 计算机网络 --万维网www
万维网是一个分布式的超媒体系统,客户程序向服务器程序发出请求,服务器程序向客户程序送回客户所需要的万维网文档.万维网必须解决的几个问题:1.怎样标志分布在整个因特网上的万维网文档?答:万维网使用统一的 ...
- C#中特性,以及应用场景(收藏链接)
1:http://www.tracefact.net/CLR-and-Framework/Reflection-Part3.aspx 2:http://www.cnblogs.com/landeanf ...
- 文件查找工具Everything小工具的使用
Everything 小工具的使用: 首先它是一款基于名称实时定位文件和目录的搜索工具,有以下几个优点: 快速文件索引 快速文件搜索 较低资源占用 轻松分享文件索引 实时跟踪文件更新 通过使用ever ...
- To discount or not to discount in reinforcement learning: A case study comparing R learning and Q learning
https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume4/kaelbling96a-html/node26.html [平均-打折奖励] Schwa ...