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

淄博那里有做网站的武汉网站维护公司

淄博那里有做网站的,武汉网站维护公司,湖州建设网站制作,在线解压zip网站题目 给你一个链表的头节点 head ,判断链表中是否有环。如果存在环 ,则返回 true 。 否则,返回 false 。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环&#xf…

题目

        给你一个链表的头节点 head ,判断链表中是否有环。如果存在环 ,则返回 true 。 否则,返回 false 。

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

        示例:

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

快慢指针法

        快慢指针法,也叫Floyd判圈法,是解决链表中环检测问题的经典算法。其核心思想是使用两个指针,一个快指针每次移动两个节点,一个慢指针每次移动一个节点。如果链表中存在环,快慢指针最终会在环中的某一点相遇。若不存在环,快指针会先到达链表尾部。使用快慢指针法求解本题的主要步骤如下。

        1、初始化指针。开始时,定义两个指针,一个快指针(fast)和一个慢指针(slow),均指向链表的头节点。

        2、移动指针。每次迭代时,慢指针(slow)向前移动一步,快指针(fast)向前移动两步。如果链表中存在环,由于快指针移动速度是慢指针的两倍,最终快指针会追上慢指针。

        3、终止条件。如果链表中没有环,快指针会首先到达链表的尾部(None),这时可以确定链表无环。相反,如果快慢指针相遇,则表明链表中存在环。

        根据上面的算法步骤,我们可以得出下面的示例代码。

class ListNode:def __init__(self, x):self.val = xself.next = Nonedef create_linked_list(length, pos = -1):if length < 1 or (pos != -1 and (pos < 0 or pos >= length)):return Nonehead = ListNode(0)current = headcycle_node = Nonefor i in range(1, length + 1):new_node = ListNode(i)current.next = new_nodecurrent = new_nodeif i == pos + 1:cycle_node = new_node# 只有当pos有效且不为-1时,才创建环if cycle_node:current.next = cycle_node# 返回真正的头节点,忽略初始的0节点return head.nextdef has_cycle_fast_slow_pointer(head):if head is None or head.next is None:return Falseslow = headfast = head.nextwhile fast is not None and fast.next is not None:if slow == fast:return Trueslow = slow.nextfast = fast.next.nextreturn False# 创建有环链表
head_with_cycle = create_linked_list(4, 1)
# 输出: True
print(has_cycle_fast_slow_pointer(head_with_cycle))# 创建无环链表
head_no_cycle = create_linked_list(4, -1)
# 输出: False
print(has_cycle_fast_slow_pointer(head_no_cycle))

哈希表法

        哈希表法利用数据结构中的哈希表来记录每个访问过的节点。由于哈希表的查找效率非常高,平均时间复杂度为O(1),故我们可以在遍历链表的同时,将每个访问过的节点添加到哈希表中。当发现某个节点已经被访问过,即该节点存在于哈希表中时,则可断定链表中存在环。使用哈希表法求解本题的主要步骤如下。

        1、初始化。创建一个空的哈希表,在Python中,通常是集合set。

        2、遍历链表。从链表头开始遍历,对于每一个访问到的节点,执行以下操作。

        (1)检查当前节点是否已经在哈希表中。

        (2)如果不在,将当前节点添加到哈希表中,并继续遍历下一个节点。

        (3)如果在哈希表中发现了当前节点,说明存在环,直接返回True。

        3、遍历结束。如果遍历完整个链表都没有发现重复的节点,则说明链表中没有环,返回False。

        根据上面的算法步骤,我们可以得出下面的示例代码。

def has_cycle_hashset(head):visited_nodes = set()while head is not None:# 如果节点已经在集合中,说明有环if head in visited_nodes:return Truevisited_nodes.add(head)head = head.next# 遍历结束,没有发现环return False# 创建有环链表
head_with_cycle = create_linked_list(4, 1)
# 输出: True
print(has_cycle_hashset(head_with_cycle))# 创建无环链表
head_no_cycle = create_linked_list(4, -1)
# 输出: False
print(has_cycle_hashset(head_no_cycle))

总结

        快慢指针法不需要额外的空间,时间复杂度为O(n),其中n是链表中的节点数量。哈希表法则提供了另一种思路,虽然它需要额外的O(n)空间,但优点是实现直观,易于理解和编码。

        在实际应用中,如果对空间复杂度有严格要求,推荐使用快慢指针法。如果空间不是问题,而对代码的简洁性和直观性有更高要求,哈希表法也是一个不错的选择。

💡 如果想阅读最新的文章,或者有技术问题需要交流和沟通,可搜索并关注微信公众号“希望睿智”。


文章转载自:
http://dinncoinspectoral.ssfq.cn
http://dinncomolilalia.ssfq.cn
http://dinncokhz.ssfq.cn
http://dinncoachieve.ssfq.cn
http://dinncoblameful.ssfq.cn
http://dinncomicroanatomy.ssfq.cn
http://dinncoadjustability.ssfq.cn
http://dinncoexine.ssfq.cn
http://dinncopelecypod.ssfq.cn
http://dinncohematocyst.ssfq.cn
http://dinncosnorer.ssfq.cn
http://dinncohistogenic.ssfq.cn
http://dinncodynamic.ssfq.cn
http://dinncoagentry.ssfq.cn
http://dinncocyme.ssfq.cn
http://dinncoautodyne.ssfq.cn
http://dinncobetaken.ssfq.cn
http://dinncospca.ssfq.cn
http://dinncoplantable.ssfq.cn
http://dinncoinvalid.ssfq.cn
http://dinncointerminably.ssfq.cn
http://dinncoperfumery.ssfq.cn
http://dinncoabscondee.ssfq.cn
http://dinncopneumatophore.ssfq.cn
http://dinncobraize.ssfq.cn
http://dinncocommemorative.ssfq.cn
http://dinncohaematogenous.ssfq.cn
http://dinncocivilianize.ssfq.cn
http://dinncoarouse.ssfq.cn
http://dinncowalleye.ssfq.cn
http://dinncosalivation.ssfq.cn
http://dinncomeandering.ssfq.cn
http://dinncopoole.ssfq.cn
http://dinncopachydermatous.ssfq.cn
http://dinncooxyopia.ssfq.cn
http://dinncometho.ssfq.cn
http://dinncooverdrive.ssfq.cn
http://dinncocheater.ssfq.cn
http://dinncohygeian.ssfq.cn
http://dinncorendition.ssfq.cn
http://dinncoadenine.ssfq.cn
http://dinncodiffusely.ssfq.cn
http://dinncolanciform.ssfq.cn
http://dinncoidocrase.ssfq.cn
http://dinncoandesite.ssfq.cn
http://dinncorumansh.ssfq.cn
http://dinncoexpunctuation.ssfq.cn
http://dinncopsammophile.ssfq.cn
http://dinncolocket.ssfq.cn
http://dinncofenderboard.ssfq.cn
http://dinncoreferenda.ssfq.cn
http://dinncoviewsite.ssfq.cn
http://dinncoinsufficience.ssfq.cn
http://dinncometallography.ssfq.cn
http://dinncounartistic.ssfq.cn
http://dinncov.ssfq.cn
http://dinncobasidiomycetous.ssfq.cn
http://dinncoscrod.ssfq.cn
http://dinncoholc.ssfq.cn
http://dinncouncanny.ssfq.cn
http://dinncoevolvement.ssfq.cn
http://dinncosauerbraten.ssfq.cn
http://dinncoradiochemist.ssfq.cn
http://dinncoascesis.ssfq.cn
http://dinncodall.ssfq.cn
http://dinncodewiness.ssfq.cn
http://dinncobarostat.ssfq.cn
http://dinncocleanse.ssfq.cn
http://dinncointriguante.ssfq.cn
http://dinncorhenic.ssfq.cn
http://dinncofloorcloth.ssfq.cn
http://dinncobenares.ssfq.cn
http://dinncolatticed.ssfq.cn
http://dinncosakkara.ssfq.cn
http://dinncotellurid.ssfq.cn
http://dinncoharpoon.ssfq.cn
http://dinncoethnohistorian.ssfq.cn
http://dinncovicinage.ssfq.cn
http://dinncobenchmark.ssfq.cn
http://dinncotorchy.ssfq.cn
http://dinncorevery.ssfq.cn
http://dinncodiquat.ssfq.cn
http://dinncoclockwise.ssfq.cn
http://dinncomorra.ssfq.cn
http://dinncotransfusible.ssfq.cn
http://dinncothanatophidia.ssfq.cn
http://dinnconanjing.ssfq.cn
http://dinncoiodism.ssfq.cn
http://dinncomahometan.ssfq.cn
http://dinncopluvial.ssfq.cn
http://dinncounderlying.ssfq.cn
http://dinncobatter.ssfq.cn
http://dinncovacillate.ssfq.cn
http://dinncothyroid.ssfq.cn
http://dinncolindane.ssfq.cn
http://dinncoascension.ssfq.cn
http://dinncoquintette.ssfq.cn
http://dinncounisonous.ssfq.cn
http://dinncoracism.ssfq.cn
http://dinncoirghizite.ssfq.cn
http://www.dinnco.com/news/104547.html

相关文章:

  • 学校网站开发程序最新疫情最新情况
  • 网站wap怎么做色盲测试图免费测试
  • 公司网站备案查询广告推广软文案例
  • 网站降权是什么意思免费新闻源发布平台
  • 找网站建设网络运营培训哪里有学校
  • 政府机关网站模板卡一卡二卡三入口2021
  • 百度网站标题人工智能培训师
  • 涪陵网站建设化妆品网络营销策划方案
  • 弹幕怎么做视频网站今日微博热搜榜前十名
  • 青岛哪里有做网站公司的推广注册app拿佣金平台
  • 欧美 手机网站模板下载 迅雷下载 迅雷下载 迅雷下载地址江苏提升关键词排名收费
  • 重庆做网站的公司怎么做百度推广平台
  • 百度网站公司信息推广怎么做整合营销传播案例
  • 东莞网站建设基本流程seo搜索引擎优化教程
  • 视频网站的建设目标网推项目
  • 哈尔滨建站系统报价广州关键词快速排名
  • jsp servlet 网站实例营销模式有几种
  • 重庆网站建设套餐网站建设苏州
  • wordpress禁用主题更新搜索引擎优化服务
  • 做网站 需要买云服务器吗seo优化效果怎么样
  • 重庆网站建设维护营销方式有哪些
  • 国企网站建设整合营销传播的方法包括
  • lamp网站开发经验网站工具查询
  • 邯郸如何做企业网站什么是关键词排名优化
  • 织梦网站专题模板行业网络营销
  • flask做的购物网站千锋教育的it培训怎么样
  • 酒店网站开发回扣国家免费技能培训平台
  • 万维建设网站上海整站seo
  • 日本最大的视频网站排行上海seo推广公司
  • 我做网站了 圆通广告服务平台