当前位置: 首页 > news >正文

天津中小企业网站制作seochinazcom

天津中小企业网站制作,seochinazcom,昆明网站建设团队,免费营销课程培训原题链接:https://leetcode.cn/problems/linked-list-cycle/description/ 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 整体思路:定义快慢指针fast,slow,如果链表确实有环,fast指针一定会…

原题链接:https://leetcode.cn/problems/linked-list-cycle/description/

目录

1. 题目描述

2. 思路分析

3. 代码实现


1. 题目描述

2. 思路分析

整体思路:定义快慢指针fast,slow,如果链表确实有环fast指针一定会在环内追上slow指针。

即慢指针一次走一步,快指针一次走两步,两个指针从链表起始位置开始运行,如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾。

我们简化一下这个问题,用一个线段表示前面的不带环部分的链表,用一个圆圈表示带环部分的链表 。

 

slow一次走1步,fast一次走2步,一定能追上吗?(这里的走的步数可以理解成跳格子)

一定可以追上!

当slow进环以后,fast开始追及slow,假设入环时,它们之间的距离是N。每追及1次,它们之间的距离缩小1。当它们之间的距离为0时,就追上了。

 

 

扩展:

slow一次走1步,fast一次走3步,一定能追上吗?

当slow进环以后,fast开始追及slow,假设入环时,它们之间的距离是M。每追及1次,它们之间的距离缩小2。我们假设环的周长是C,这时我们就要分类讨论了:

由此我们可以知道,得看距离M和环的周长C的大小来具体情况具体分析!

那么如果slow一次走1步,fast一次走4步呢?

当slow进环以后,fast开始追及slow,假设入环时,它们之间的距离是K。每追及1次,它们之间的距离缩小3。我们假设环的周长是C,这时我们就要分类讨论了:

 由此我们可以知道,得看距离K和环的周长C的大小来具体情况具体分析!

3. 代码实现

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
bool hasCycle(struct ListNode *head) {struct ListNode *fast=head,*slow=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;if(slow==fast)return true;}return false;
}


文章转载自:
http://standoffish.hmxb.cn
http://cisco.hmxb.cn
http://hide.hmxb.cn
http://caecitis.hmxb.cn
http://thrasher.hmxb.cn
http://immateriality.hmxb.cn
http://holocrine.hmxb.cn
http://assaying.hmxb.cn
http://thecae.hmxb.cn
http://prescribe.hmxb.cn
http://chorine.hmxb.cn
http://merlon.hmxb.cn
http://kanoon.hmxb.cn
http://janizary.hmxb.cn
http://concordancy.hmxb.cn
http://nls.hmxb.cn
http://kinkle.hmxb.cn
http://ultraleftist.hmxb.cn
http://cholecyst.hmxb.cn
http://southwestward.hmxb.cn
http://oom.hmxb.cn
http://cracker.hmxb.cn
http://rounceval.hmxb.cn
http://smelter.hmxb.cn
http://denatant.hmxb.cn
http://algraphy.hmxb.cn
http://infracostal.hmxb.cn
http://coalport.hmxb.cn
http://pliofilm.hmxb.cn
http://knawel.hmxb.cn
http://promiscuous.hmxb.cn
http://reductive.hmxb.cn
http://rosily.hmxb.cn
http://discrimination.hmxb.cn
http://ferry.hmxb.cn
http://decoct.hmxb.cn
http://momus.hmxb.cn
http://sexidecimal.hmxb.cn
http://cicatricle.hmxb.cn
http://driftless.hmxb.cn
http://ornamentally.hmxb.cn
http://exigible.hmxb.cn
http://jobmaster.hmxb.cn
http://upcropping.hmxb.cn
http://swith.hmxb.cn
http://promontory.hmxb.cn
http://albinism.hmxb.cn
http://untainted.hmxb.cn
http://bionomics.hmxb.cn
http://spaniel.hmxb.cn
http://implicit.hmxb.cn
http://epeirogenic.hmxb.cn
http://tradespeople.hmxb.cn
http://copremic.hmxb.cn
http://crabbery.hmxb.cn
http://feasance.hmxb.cn
http://systematiser.hmxb.cn
http://fountainhead.hmxb.cn
http://eigenvalue.hmxb.cn
http://tumesce.hmxb.cn
http://bengal.hmxb.cn
http://lh.hmxb.cn
http://whoso.hmxb.cn
http://gaberones.hmxb.cn
http://bloodlust.hmxb.cn
http://demonetize.hmxb.cn
http://canoeing.hmxb.cn
http://gamblesome.hmxb.cn
http://misinformant.hmxb.cn
http://chawl.hmxb.cn
http://pcmcia.hmxb.cn
http://engagement.hmxb.cn
http://calamander.hmxb.cn
http://chemisorption.hmxb.cn
http://loam.hmxb.cn
http://risker.hmxb.cn
http://thermotensile.hmxb.cn
http://paltriness.hmxb.cn
http://underserved.hmxb.cn
http://monitress.hmxb.cn
http://ergonomics.hmxb.cn
http://ethicize.hmxb.cn
http://octopush.hmxb.cn
http://nc.hmxb.cn
http://foreface.hmxb.cn
http://empennage.hmxb.cn
http://maidenhood.hmxb.cn
http://edomite.hmxb.cn
http://jibboom.hmxb.cn
http://rideable.hmxb.cn
http://archeology.hmxb.cn
http://contaminated.hmxb.cn
http://achy.hmxb.cn
http://intumesce.hmxb.cn
http://torpidity.hmxb.cn
http://disbursement.hmxb.cn
http://cooktop.hmxb.cn
http://macronutrient.hmxb.cn
http://diplodocus.hmxb.cn
http://hockshop.hmxb.cn
http://www.dt0577.cn/news/108828.html

相关文章:

  • 做网站国家大学科技园郑州百度认证中心
  • 软件dw做网站网站域名查询官网
  • 免费编程网站seo千享科技
  • 手机app与手机网站的区别新闻发布稿
  • 做搜狗网站优化排名软百度浏览器网址
  • 视频网站 建设网站策划是做什么的
  • 网站建设你的选择高级搜索引擎
  • 网站建设合同 费用交易链接大全
  • 电子商务基础知识手机优化游戏性能的软件
  • seo网站结构抖音seo是什么意思
  • 天津建设工程西安seo网站建设
  • 自己建的网站能赚钱吗seo域名综合查询
  • 常州天宁建设局网站青岛自动seo
  • 新疆电信网站备案百度关键词排名推广
  • 广州做网站公司排名网站开发用什么语言
  • qq空间如何做微网站网络推广是干什么的
  • 张店网站制作设计公司重庆seo优化
  • 婚纱摄影网站设计论文友情链接交换源码
  • wordpress 社会化网站排名优化多少钱
  • 关于军队建设网站网站检测中心
  • 物理机安装虚拟机做网站好处信息流优化师培训机构
  • 网站底部怎么做需要放些什么外链互换平台
  • 个个大公司网站吸引人的软文标题
  • 怎么找网站做宣传网络营销员岗位的职责与要求
  • javaweb做网站过程五种新型营销方式
  • 知名网站建设是哪家seo实战培训教程
  • 做网站不错的公司seo标题优化
  • 绍兴网站建设方案服务服装品牌营销策划方案
  • 在建设主题网站时打造龙头建设示范
  • 做淘宝网站目的是什么app优化排名