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 ...
随机推荐
- Disruptor极速队列
参考:http://www.cnblogs.com/haiq/p/4112689.html Disruptor 是线程内通信框架,用于线程里共享数据.LMAX 创建Disruptor作为可靠消息架构的 ...
- js使用html2canvas 生成图片然后下载
1:html2canvas官网 首先去官网把这个JS下载下来 <!DOCTYPE html> <html lang="en"> <head> & ...
- js替换指定位置字符串
var str='QWER';//替换WE newstr=replacepos(str,1,2,'XX'); console.log(newstr);//QXXR; function replacep ...
- SpringBoot集成Junit
1.在pom.xml下添加Junit依赖: <!--添加junit环境的jar包--> <dependency> <groupId>org.springframew ...
- SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志
目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...
- 5.SSH 免密码登陆
SSH 免密码登陆 serverA 机器上 userA 用户,想要免密钥登陆到serverB机器上 1.集群中的所有机器 安装ssh 服务端sudo apt-get updatesudo apt-ge ...
- Docker-Docker与IPV6
公司计划在2020年前完成IPV6化改造,于是我先行查阅了一些资料了解Docker进行IPv6化的可能性. 预计明年正式开始测试. 方法一.使容器中的服务支持IPv6地址 不为容器中的服务特别分配IP ...
- Shell编程—正则表达式
1什么是正则表达式 1.1定义 正则表达式是你所定义的模式模板,Linux工具可以用它来过滤文本.Linux 工具(比如sed编辑器或gawk程序)能够在处理数据时使用正则表达式对数据进行模式匹配. ...
- Qt 让Label显示图片并把图片居中
Qt 让Label显示图片并把图片居中 QPixmap image("./13.jpg"); QPixmap fitpixmap=image.scaled(ui->lab ...
- 3个必备cookie实用方法
今天跟大家介绍一下三种cookie的使用方法,selenium提供了我们add_cookie()方法来跳过验证码直接登录的方法.我们现在以博客园登录为例,都知道现在博客园登录要拼图验证. 先在网页打开 ...