问题描述:

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 poswhich 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的更多相关文章

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

  2. [LeetCode] Linked List Cycle II 单链表中的环之二

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

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

  4. [算法][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 ...

  5. [Leetcode] Linked list cycle ii 判断链表是否有环

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

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

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

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

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

随机推荐

  1. hdu 5071 Chat-----2014acm亚洲区域赛鞍山 B题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. zabbix-agent active 配置自动探测

    1. zabbix-agent 被动模式配置文件: PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_a ...

  3. Micro Python:运行在微控制器上的Python

    Micro Python运行在微控制器上的Python.遵守MIT协议.由剑桥大学的理论物理学家乔治·达明设计.和Arduino类似,但Micro Python更强大. Micro Python的软件 ...

  4. 解决UICollectionView的Cell复用引起的布局混乱问题

    解决UICollectionView的Cell复用引起的布局混乱问题   问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊

  5. linux c编程:网络编程

    在网络上,通信服务都是采用C/S机制,也就是客户端/服务器机制.流程可以参考下图: 服务器端工作流程: 使用socket()函数创建服务器端通信套接口 使用bind()函数将创建的套接口与服务器地址绑 ...

  6. 【linux】让普通用户执行root的程序

    再有些时候,比如zabbix监控中,需要使用netstat命令查看当前网络链接状态,但是zabbix用户没有权限执行netstat,会导致监控失败,为此使用如下即可解决 chmod +s /bin/n ...

  7. 在linux 中卸载Mysql

    一.通用的mysql卸载方式 1.查看系统中是否已经安装了mysql 命令:rpm -qa|grep -i mysql如果有显示msql的安装列表,代表已经安装了. 2.停止mysql服务.删除之前安 ...

  8. SQL语法之初级增删改查

    SQL语法之初级增删改查 1.增 1.1插入单行 INSERT INTO [表名](列名) VALUES(列值) 语法如下: INSERT INTO bsp_Nproductclass(guid,pi ...

  9. 移动端 (H5) 调试工具 -- vconsole

    最近在改一个移动端项目,在手机上调试贼头疼,什么日志都看不到,分析不了bug问题. 然后我同事给我介绍了一个移动端的调试神器 -- vconsole 有了这个神器,领导再也不用担心我的工作啦!!! 0 ...

  10. Python爬虫 —— 抓取美女图片(Scrapy篇)

    杂谈: 之前用requests模块爬取了美女图片,今天用scrapy框架实现了一遍. (图片尺度确实大了点,但老衲早已无恋红尘,权当观赏哈哈哈) Item: # -*- coding: utf-8 - ...