python判断链表是否有环的实例代码


Posted in Python onJanuary 31, 2020

先看下实例代码:

class Node:
  def __init__(self,value=None):
    self.value = value
    self.next = None

class LinkList:
  def __init__(self,head = None):
    self.head = head

  def get_head_node(self):
    """
    获取头部节点
    """
    return self.head
    
  def append(self,value) :
    """
    从尾部添加元素
    """  
    node = Node(value = value) 
    cursor = self.head 
    if self.head is None:
      self.head = node
    else:  
      while cursor.next is not None:
        cursor = cursor.next
    
 
      cursor.next = node
      if value==4:
        node.next = self.head
  
  def traverse_list(self):
    head = self.get_head_node()
    cursor = head
    while cursor is not None:
      print(cursor.value)
      cursor = cursor.next
    print("traverse_over") 
    
  def hasCycle(self, head):
    """
    :type head: ListNode
    :rtype: bool
    """
    slow=fast=head
    while slow and fast and fast.next:
      slow = slow.next
      fast = fast.next.next
      if slow is fast:
        return True
    return False
  

  
def main():
  l = LinkList()
  l.append(1)
  l.append(2)
  l.append(3)
  l.append(4)
  head = l.get_head_node()
  print(l.hasCycle(head))
  #l.traverse_list()


if __name__ == "__main__":
  main()

知识点思考

判断一个单链表是否有环,

可以用 set 存放每一个 节点, 这样每次 访问后把节点丢到这个集合里面.

其实 可以遍历这个单链表, 访问过后,

如果这个节点 不在 set 里面, 把这个节点放入到 set 集合里面.

如果这个节点在 set 里面 , 说明曾经访问过, 所以这个链表有重新 走到了这个节点, 因此一定有环

如果链表都走完了, 把所有的节点都放完了. 还是没有重复的节点, 那说明没有环.

以上就是本次介绍的全部相关知识点内容,感谢大家的学习和对三水点靠木的支持。

Python 相关文章推荐
Python3爬虫学习入门教程
Dec 11 Python
详解python持久化文件读写
Apr 06 Python
python批量修改ssh密码的实现
Aug 08 Python
使用python批量修改文件名的方法(视频合并时)
Mar 24 Python
python实现生成Word、docx文件的方法分析
Aug 30 Python
简单瞅瞅Python vars()内置函数的实现
Sep 27 Python
Python生成词云的实现代码
Jan 14 Python
python torch.utils.data.DataLoader使用方法
Apr 02 Python
Python Flask框架实现简单加法工具过程解析
Jun 03 Python
Python爬取科目四考试题库的方法实现
Mar 30 Python
如何用python插入独创性声明
Mar 31 Python
Python的这些库,你知道多少?
Jun 09 Python
python爬取王者荣耀全皮肤的简单实现代码
Jan 31 #Python
Python进阶之迭代器与迭代器切片教程
Jan 29 #Python
Python列表list操作相关知识小结
Jan 29 #Python
给Python学习者的文件读写指南(含基础与进阶)
Jan 29 #Python
40个你可能不知道的Python技巧附代码
Jan 29 #Python
你可能不知道的Python 技巧小结
Jan 29 #Python
Python如何通过Flask-Mail发送电子邮件
Jan 29 #Python
You might like
php设计模式 Facade(外观模式)
2011/06/26 PHP
浅析php变量修饰符static的使用
2013/06/28 PHP
Zend Framework动作助手Url用法详解
2016/03/05 PHP
PHP中ltrim()函数的用法与实例讲解
2019/03/28 PHP
CSS中一些@规则的用法小结
2021/03/09 HTML / CSS
js报错 Object doesn't support this property or method的原因分析
2011/03/31 Javascript
JS实现时间格式化的方式汇总
2013/10/16 Javascript
node.js cookie-parser 中间件介绍
2016/06/06 Javascript
js检查是否关闭浏览器的方法
2016/08/02 Javascript
jquery.zclip轻量级复制失效问题
2017/01/08 Javascript
JavaScript使用原型和原型链实现对象继承的方法详解
2017/04/05 Javascript
javascript如何用递归写一个简单的树形结构示例
2017/09/06 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
vue移动端的左右滑动事件详解
2020/06/17 Javascript
[01:15:18]2014 DOTA2国际邀请赛中国区预选赛 LGD VS Speed Gaming.cn
2014/05/22 DOTA
[02:28]DOTA2 2015国际邀请赛中国区预选赛首日现场百态
2015/05/26 DOTA
python概率计算器实例分析
2015/03/25 Python
python读取TXT到数组及列表去重后按原来顺序排序的方法
2015/06/26 Python
尝试用最短的Python代码来实现服务器和代理服务器
2016/06/23 Python
python清理子进程机制剖析
2017/11/23 Python
python正则实现计算器功能
2017/12/14 Python
django 解决manage.py migrate无效的问题
2018/05/27 Python
Python 普通最小二乘法(OLS)进行多项式拟合的方法
2018/12/29 Python
Python 旋转打印各种矩形的方法
2019/07/09 Python
详解快速开发基于 HTML5 网络拓扑图应用
2018/01/08 HTML / CSS
Tiqets英国:智能手机上的文化和娱乐门票
2019/07/10 全球购物
创新型城市实施方案
2014/03/06 职场文书
助人为乐好少年事迹材料
2014/08/18 职场文书
革命英雄事迹演讲稿
2014/09/13 职场文书
2014年团支部年度工作总结
2014/12/24 职场文书
2015年团队工作总结范文
2015/05/04 职场文书
办公经费申请报告
2015/05/15 职场文书
法院执行局工作总结
2015/08/11 职场文书
Css预编语言及区别详解
2021/04/25 HTML / CSS
解决Go gorm踩过的坑
2021/04/30 Golang
Android存储中最基本的文件存储方式
2022/04/30 Java/Android