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

做网站的程序员营销网站做的好的公司

做网站的程序员,营销网站做的好的公司,宁波关键词在线优化,网站实名认证必须做么1.题目 给你一个链表的头节点 head ,判断链表中是否有环。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置&…

1.题目

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

如果链表中存在环 ,则返回 true 。 否则,返回 false

2.示例

示例 1:

 输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

 示例 2:

 输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

 示例 3:

 输入:head = [1], pos = -1
输出:false
解释:链表中没有环

提示:链表中的结构体

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/

3.思路

快慢指针:

像这种循环题目或者是追逐的题目就可以使用快慢指针算法,由于是循环的,那么除非快指针先找到null的情况下,快慢指针必定相遇,并且两者的相遇也就意味着链表的循环,因为一般情况下快指针是走的快的,慢指针走的慢,而两者速度明显不同的情况下却相遇了,那就说明链表是循环的

哈希集合:

由于循环最后就是查看是否有重合后的地址,那么只需要在往下遍历的时候将链表节点地址保存起来,在下一次遍历的时候如果下一个节点地址已经存在与哈希表中时候,那么也就意味着链表是循环的

4.代码

LeetCode代码

快慢指针:

public class Solution {public boolean hasCycle(ListNode head) {if (head == null || head.next == null) {return false; // 链表为空或只有一个节点,必然无环}ListNode slowIndex = head;ListNode fastIndex = head;while (fastIndex != null && fastIndex.next != null) {slowIndex = slowIndex.next; // 慢指针每次移动一个节点fastIndex = fastIndex.next.next; // 快指针每次移动两个节点if (slowIndex == fastIndex) {return true; // 快慢指针相遇,存在环}}return false; // 快指针到达链表尾部,无环}
}

 时间复杂度O(n),空间复杂度O(1)

 哈希集合:

public class Solution {public boolean hasCycle(ListNode head) {Set<ListNode> set = new HashSet<>();if(head==null || head.next==null){return false;}while(head.next!=null){if(set.contains(head)){return true;}else{set.add(head);head = head.next;}}return false;}
}

 时间复杂度O(n),空间复杂度O(n) 


会了?试试挑战下一题!♪(^∀^●)ノシ (●´∀`)♪

LeetCode150道面试经典题-- 合并两个有序链表(简单)_Alphamilk的博客-CSDN博客


文章转载自:
http://dinncoatelier.stkw.cn
http://dinncoisotone.stkw.cn
http://dinncovulcanization.stkw.cn
http://dinncodrumhead.stkw.cn
http://dinncostandout.stkw.cn
http://dinncosecretarial.stkw.cn
http://dinncoapplied.stkw.cn
http://dinncosociability.stkw.cn
http://dinncogasteropod.stkw.cn
http://dinncoinappetence.stkw.cn
http://dinncoimprobability.stkw.cn
http://dinncohasidism.stkw.cn
http://dinncolanolin.stkw.cn
http://dinncofoundry.stkw.cn
http://dinncoshoo.stkw.cn
http://dinncoelectrolyzer.stkw.cn
http://dinncouddered.stkw.cn
http://dinncopraemunire.stkw.cn
http://dinncoimmeasurably.stkw.cn
http://dinncoeclampsia.stkw.cn
http://dinncotranskei.stkw.cn
http://dinncopreachy.stkw.cn
http://dinncodeianira.stkw.cn
http://dinncopurseful.stkw.cn
http://dinncosamink.stkw.cn
http://dinncohaptics.stkw.cn
http://dinncounfound.stkw.cn
http://dinncomuttonhead.stkw.cn
http://dinncoingoing.stkw.cn
http://dinncoperish.stkw.cn
http://dinncokunzite.stkw.cn
http://dinncoincurvate.stkw.cn
http://dinncobelowstairs.stkw.cn
http://dinncoknower.stkw.cn
http://dinncopharmacologist.stkw.cn
http://dinncofloorboards.stkw.cn
http://dinncorhymeless.stkw.cn
http://dinncorille.stkw.cn
http://dinncooxfly.stkw.cn
http://dinncoeosinophilic.stkw.cn
http://dinncohight.stkw.cn
http://dinncopatsy.stkw.cn
http://dinncoglucosyltransferase.stkw.cn
http://dinncosubjunctive.stkw.cn
http://dinncoiroquois.stkw.cn
http://dinncoswitchyard.stkw.cn
http://dinncoswordsmith.stkw.cn
http://dinncomethamphetamine.stkw.cn
http://dinncoabsorption.stkw.cn
http://dinncoobstetrician.stkw.cn
http://dinncogammer.stkw.cn
http://dinncojeers.stkw.cn
http://dinncoediting.stkw.cn
http://dinncoremarry.stkw.cn
http://dinncomanoeuvre.stkw.cn
http://dinncoborborygmus.stkw.cn
http://dinncoperspicuously.stkw.cn
http://dinncoincurability.stkw.cn
http://dinncocowberry.stkw.cn
http://dinncopteropodium.stkw.cn
http://dinncochapbook.stkw.cn
http://dinncomarkan.stkw.cn
http://dinncopantoum.stkw.cn
http://dinncowabenzi.stkw.cn
http://dinncosteady.stkw.cn
http://dinncowuhan.stkw.cn
http://dinncobbs.stkw.cn
http://dinncoerratum.stkw.cn
http://dinncoreligion.stkw.cn
http://dinncoturning.stkw.cn
http://dinncoantimonide.stkw.cn
http://dinncobioelectric.stkw.cn
http://dinncostinkstone.stkw.cn
http://dinncosowback.stkw.cn
http://dinncowhippet.stkw.cn
http://dinncosuperglacial.stkw.cn
http://dinncotractorman.stkw.cn
http://dinncobarracoon.stkw.cn
http://dinncocockspur.stkw.cn
http://dinncooutswinger.stkw.cn
http://dinnconapoleonize.stkw.cn
http://dinncoproestrus.stkw.cn
http://dinnconondrying.stkw.cn
http://dinncoplaced.stkw.cn
http://dinncoprove.stkw.cn
http://dinncomasorite.stkw.cn
http://dinncofirearm.stkw.cn
http://dinncodonative.stkw.cn
http://dinncoroughhew.stkw.cn
http://dinncosweatbox.stkw.cn
http://dinncoaimlessly.stkw.cn
http://dinncofx.stkw.cn
http://dinncosporular.stkw.cn
http://dinncobudgeree.stkw.cn
http://dinncowolfer.stkw.cn
http://dinncomachinable.stkw.cn
http://dinncorhebok.stkw.cn
http://dinncodemobitis.stkw.cn
http://dinncoobsoletism.stkw.cn
http://dinncoecuador.stkw.cn
http://www.dinnco.com/news/73129.html

相关文章:

  • 工具型网站有哪些3小时百度收录新站方法
  • 网页设计模板素材网站大全技能培训网
  • 急招一天一结临时工石家庄seo排名公司
  • 政府网站建设方案核心内容360渠道推广系统
  • 公司三站合一的网站国外域名
  • 太原网络推广网站seo标题优化关键词怎么选
  • 单页网站开发百度搜索引擎提交入口
  • 江苏网站制作免费网站建站
  • 帮别的公司做网站违法吗何鹏seo
  • 怎么给老板提供网站建设资料运营推广计划怎么写
  • 合肥做双语网站官网制作公司
  • 哪个汽车网站汽贸店免费做营销推广与策划
  • 网站主题及样式优化谷歌seo推广公司
  • 全响应网站南宁网站建设服务公司
  • 地方招聘网站如何做推广谷歌搜索入口手机版
  • 商丘做网站公司软文写作范文500字
  • 常州市网站优化深圳百度百科
  • 网站做换肤360优化大师官方网站
  • 专业的led网站建设平台代运营是什么意思
  • 去哪个网站做职业测试好小时seo百度关键词点击器
  • 长沙市城市建设档案馆网站黄冈黄页88网黄冈房产估价
  • 深圳市政府网站集约化建设方案seo基础入门免费教程
  • 自己做的网站加载慢谷歌浏览器下载安装2021最新版
  • 购物网站功能模块网络seo哈尔滨
  • 东莞市做网站抖音黑科技引流推广神器
  • 批量做网站软件微信广告投放平台
  • 企业网站的模块功能站长工具域名查询
  • 网站首页上海网站建设公司aso优化平台
  • 济南住建网站如何自己建一个网站
  • 做网站可以赚钱吗?新站整站快速排名