python判断链表是否有环
思路:使用快慢指针,快指针每次走两步,慢指针每次走一步,如果有环,则一定会快慢指针指向同一结点;
假设环的长度为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判断链表是否有环的更多相关文章
- 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 ...
- leetCode-linkedListCycle判断链表是否有环
题目 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- [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 ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- 判断链表是否有环(Java实现)
判断给定的链表中是否有环.如果有环则返回true,否则返回false. 解题思路:设置两个指针,slow和fast,fast每次走两步,slow每次走一步,如果有环的话fast一定会追上slow,判断 ...
- 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 ...
随机推荐
- Cheese
题面: 现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪 中间有许多 半径相同 的球形空洞.我们可以在这块奶酪中建立空间坐标系,在坐标系中, 奶酪的下表面为z=0,奶酪的上表 ...
- 笔记:phpstudy、虚拟机CentOS安装、Linux命令
一.phpstudy 1.phpstudy实现w(Windows)a(Apache)m(Mysql)p(php)环境 Apache 用来发布Web服务 80端口 MySQL 开源的建议灵活的 ...
- 无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
状况 如果在使用 vue 初始化项目的时候提示: vue : 无法将“vue”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...
- Python开发的入门教程(二)-List和Tuple类型
介绍 本文主要介绍Python中List和Tuple类型的基本知识和使用. Python创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的 ...
- 使用client-go自定义开发Kubernetes
参考链接:使用client-go自定义开发Kubernetes 1.本地运行 apiserver demo [root@wangjq demo]# apiserver-boot run local / ...
- 企业级Registry仓库Harbor的部署与简介
Harbor 是Vmware公司开源的企业级Docker Registry管理项目,开源项目地址:https://github.com/vmware/harbor Harbor的所有组件都在Docke ...
- 常用生成模型代码大全(pytorch/tensorflow)
感谢大佬开源分享 代码详见:https://github.com/wiseodd/generative-models
- Spark本地配置
1. 下载解压安装包 tar -xvf spark-2.0.2-bin-hadoop2.6.tgz tar -xvf scala-2.11.8.tgz 2. 修改Spark配置文件 cd spark- ...
- C#串口通讯,复制粘贴就可用,仅仅介绍怎样最快的搭建一个串口通讯,异常拦截等等需要自己加上
using System; using System.Collections.Generic; using System.IO.Ports; using System.Text; //串口通讯类 pu ...
- Angular 之我见
很久没有写过技术软文了,虽然 Angular 发布已有四年,得到了越来越多人的关注,但是仍然有很多人不分青红皂白的进行诋毁.我打算结合自己的经历从客观的角度说聊一聊我眼中的 Angular. 本人刚做 ...