思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点;
假设环的长度为n,先让一个指针走n步,另一个再开始走,当他们指针指向同一结点时,该结点就是环入口点
(在快慢指针相遇之后,慢指针指向表头,快指针留在相遇点,二者以每次一步走直到相遇,该相遇点就是环入口结点);

找到环入口结点之后,从入口结点开始遍历,每次遍历长度加一,如果下个结点等于入口结点,则返回长度

class ListNode(object):
def __init__(self, dataval):
self.dataval = dataval
self.next = None class JudgeLinkRing(object):
def is_link_ring(self,head):
if head is None or head.next is None:
return
slow,fast=head,head
while fast and fast.next:
slow=slow.head
fast=fast.next.next
if slow==fast:
slow=head
while slow !=fast:
slow=slow.next
fast=fast.next
fast1=fast
fast=fast.next
n=1
while fast1 !=fast
fast=fast.next
n=n+1 return slow,n return

python判断链表是否有环的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

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

  2. leetCode-linkedListCycle判断链表是否有环

    题目 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ...

  3. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

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

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  5. 141 Linked List Cycle(判断链表是否有环Medium)

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

  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] Linked list cycle ii 判断链表是否有环

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

  8. 判断链表是否有环(Java实现)

    判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...

  9. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

随机推荐

  1. Atomic原子类

    Atomic原子类 Atomic原子类位于并发包java.util.concurrent下的java.util.concurrent.Atomic中. 1. 原子更新基本类型类 使用原子方式更新基本数 ...

  2. Redis设计与实现——独立功能的实现

    发布和订阅 频道的订阅和退订 struct redisServer{ //键是被订阅者频道 ,键是一个链表,记录所有订阅这个频道的客户端 dict *publish_channels } 订阅实现: ...

  3. Vue.js中传值给子部件及触发动作的问题

    最近研究一个用vue.js做的程序并修改增加功能.其中用到传值给子部件等问题. template中有个子部件: <template> ...... <child-form  v-if ...

  4. 性能分析(7)- 未利用系统缓存导致 I/O 缓慢案例

    性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前提 前面有学到 Buffer 和 Cache 的 ...

  5. Oracle 根据单元格内容不同来判断不同条件

    select xxx,xxx from xxx where decode(AAA,0,BBB,1,CCC)='12345' 如果AAA字段等于0则AAA=''12345' 否则如果BBB字段等于1则B ...

  6. 『JWT』,你必须了解的认证登录方案

    我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农! 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在 ...

  7. nova 数据库中删除虚机

    Database changed MariaDB [nova]> SET FOREIGN_KEY_CHECKS=; Query OK, rows affected (0.000 sec) Mar ...

  8. new操作符创建对象的四个步骤

    new操作符创建对象可以分为以下四个步骤: 创建一个空对象 将所创建对象的__proto__属性值设为构造函数的prototype的属性值 执行构造函数中的代码,构造函数中的this指向该对象 返回对 ...

  9. 90%的开发都没搞懂的CI和CD!

    据IDC统计,2017年,DevOps的全球软件市场已达到约39亿美元的水平,预计到2022年市场将达到80亿美元左右! 在敏捷软件开发环境中,工作模型和操作需要对公司不断变化的需求具有超级灵活的能力 ...

  10. 小白一样能建站——winser2012 IIS8.0搭建基本的网站

    在window server 2012环境下,搭建一个基本的 网站.能够使用即可. 打开服务器管理器 添加角色和功能 默认下一步 下一步, 下一步,选择web服务器 添加功能 下一步, 下一步,不安装 ...