news 2026/6/25 15:34:30

LeetCode142:巧解链表环入口(多解)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LeetCode142:巧解链表环入口(多解)

题目LeetCode142

给定一个链表的头节点head,返回链表开始入环的第一个节点。如果链表无环,则返回null

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

不允许修改链表

示例 1:

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

Python解法

1.哈希表

class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: visited = [] while head: if head in visited: return head else: visited.append(head) head = head.next return None

逐个遍历,重复直接为所求

2.快慢指针

class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return None slow = head fast = head while fast: slow = slow.next if fast.next: fast = fast.next.next else: return None if slow == fast: ptr = head while ptr != slow: ptr = ptr.next slow = slow.next return ptr return None

解法二的关键在于

if slow == fast: ptr = head while ptr != slow: ptr = ptr.next slow = slow.next return ptr

Java解法

1.哈希表

public class Solution{ public ListNode detectCycle(ListNode head){ ListNode pos = head; Set<ListNode> visited = new HashSet<ListNode>(); while(pos != null){ if(visited.contains(pos)){ return pos; }else{ visited.add(pos); } pos = pos.next; } return null; } }

2.快慢指针

public class Solution { public ListNode detectCycle(ListNode head) { if(head == null){ return null; } ListNode slow = head; ListNode fast = head; while(fast != null){ slow = slow.next; if(fast.next != null){ fast = fast.next.next; }else{ return null; } if(slow == fast){ ListNode ptr = head; while(slow != ptr){ slow = slow.next; ptr = ptr.next; } return ptr; } } return null; } }

C++解法

1.哈希表

class Solution{ public: ListNode *detectCycle(ListNode *head){ ListNode *pos = head; unordered_set<ListNode *> visited; while(pos != nullptr){ if(visited.count(pos)){ return pos; }else{ visited.insert(pos); } pos = pos->next; } return nullptr; } };

2.快慢指针

class Solution{ public: ListNode *detectCycle(ListNode *head){ if(head == nullptr){ return nullptr; } ListNode *slow = head; ListNode *fast = head; while(fast != nullptr){ slow = slow->next; if(fast->next != nullptr){ fast = fast->next->next; }else{ return nullptr; } if(slow == fast){ ListNode *ptr = head; while(slow != ptr){ slow = slow->next; ptr = ptr->next; } return ptr; } } return nullptr; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/25 15:32:35

轻松搞定论文:6款2026年靠谱AI写论文工具深度横评

在学术写作面临全新挑战的今天&#xff0c;AI工具正从辅助角色演变为重要的生产力引擎。针对免费、好用且能提供真实引用支持的核心需求&#xff0c;经过对市面上主流工具的深入测试与分析&#xff0c;我们发现表现突出的工具有&#xff1a;千笔AI、ChatGPT、Claude、文心一言、…

作者头像 李华
网站建设 2026/6/25 15:28:48

101 01 黄大年茶思屋榜文101期 第1题 内存友好的高效MoE架构

摘要针对传统MoE大模型推理存在全专家常驻内存、RAM占用冗余度极高、逐Token动态路由频繁IO切换、终端功耗超标、精度与资源开销无法双向平衡的刚性工程缺陷&#xff0c;本文基于工业落地优先、鲁棒性优先、性价比优先原则&#xff0c;采用会话级专家静态锁定分层内存分级驻留场…

作者头像 李华
网站建设 2026/6/25 15:28:05

10分钟掌握xdotool:Linux桌面自动化的终极免费神器

10分钟掌握xdotool&#xff1a;Linux桌面自动化的终极免费神器 【免费下载链接】xdotool fake keyboard/mouse input, window management, and more 项目地址: https://gitcode.com/gh_mirrors/xd/xdotool 你是否厌倦了每天重复点击相同的按钮&#xff1f;是否希望让电…

作者头像 李华
网站建设 2026/6/25 15:27:15

01-Vue2 入门与环境搭建

Vue2 入门与环境搭建从理解 Vue 的核心理念到搭建第一个可运行的 Vue 应用&#xff0c;本章将带你完成 Vue2 学习之旅的第一步。一、前言 Vue.js 是一款用于构建用户界面的渐进式 JavaScript 框架。所谓"渐进式"&#xff0c;意味着你可以根据项目需求逐步引入 Vue 的…

作者头像 李华
网站建设 2026/6/25 15:26:22

HumanEgo 论文主实验硬件解析:Trossen WidowX AI 双臂工作站实操方案

当前视觉操作&#xff08;VLA&#xff09;、具身模仿学习算法普遍存在数据采集门槛高的问题&#xff1a;主流训练方案依赖大量机器人遥操作轨迹&#xff0c;单套双臂设备采集数十小时数据&#xff0c;人力与硬件损耗成本高昂&#xff1b;同时算法高度绑定特定机械臂&#xff0c…

作者头像 李华