Lintcode103 Linked List Cycle || solution 题解
来源:互联网 发布:ps3验证游戏数据 编辑:程序博客网 时间:2024/06/16 09:58
【题目描述】
Given a linked list, return the node where the cycle begins.
If there is no cycle, returnnull.
给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null。
【题目链接】
www.lintcode.com/en/problem/linked-list-cycle-ii/
【题目解析】
此题不仅要求判断是否存在环,同时还需要在存在环的情况下找出环的起始节点。这就比I要难一些。最开始我想到的方法还是跟上题类似,一个fast ,每次移动两步,一个slow,每次移动一步。两个指针不仅要向前移动,同时还需要记录各自走的步数(fastCount和slowCount)。当相遇的时候,fastCount减去slowCount就是换的长度(假设这个长度的len)。这个时候让fast和slow重新指向head节点。然后先让fast指针向前移动len步。之后fast和slow再同时移动,两个每次均移动一步。当两者相遇的时候就是环的其实节点。
【参考答案】
www.jiuzhang.com/solutions/linked-list-cycle-ii/
阅读全文
0 0
- Lintcode103 Linked List Cycle || solution 题解
- Lintcode102 Linked List Cycle solution 题解
- [LeetCode] Linked List Cycle, Solution
- LeetCode题解:Linked List Cycle
- [题解][LeetCode][Linked List Cycle]
- LeetCode题解:Linked List Cycle
- LeetCode题解:Linked List Cycle
- [LeetCode] Linked List Cycle II, Solution
- [LeetCode] Linked List Cycle II Solution
- [LeetCode] Linked List Cycle II, Solution
- LeetCode Linked List Cycle & Linked List Cycle II题解
- Lintcode35 Reverse Linked List solution 题解
- Lintcode36 Reverse Linked List II solution 题解
- Lintcode35 Reverse Linked List solution 题解
- [题解][LeetCode][Linked List Cycle II]
- LeetCode 题解(47): Linked List Cycle
- LeetCode141之Linked List Cycle java题解
- LeetCode题解——Linked List Cycle
- 关于AndroidStudio的快捷键使用
- CodeForces
- 排序算法五——堆排序
- 8.9上课感悟
- 接口作为类型使用
- Lintcode103 Linked List Cycle || solution 题解
- Java并发编程:Synchronized底层优化(偏向锁、轻量级锁)
- Mysql 练习题 加Mysql的表
- 【51nod 教程】矩阵取数问题(动态规划)
- 渐进增强和优雅降级之间有什么不同?
- 如何用SpringBoot框架来接收multipart/form-data文件
- poj 3126 Prime Path bfs
- Oracle PL/SQL开发基础(第二十九弹:异常处理简介)
- 普通短信发送流程