leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up: Can you solve it without using extra space?
思路:
如果前面Linked List Cycle的问题已经解决,再扩展一下就可以了。如果一步走两步走的方法在某个点交汇了,那么这个点肯定在环内。那么算出环的长度n,令某个节点从head出发先走n步,然后与head节点一起前进,那么交汇点就是环的起点。
1 class Solution { 2 public: 3 ListNode *detectCycle(ListNode *head) { 4 if (NULL == head) { 5 return NULL; 6 } 7 8 ListNode *curr = head, *next = head->next; 9 10 while (true) { 11 curr = curr->next; 12 13 int steps = 0; 14 15 while ((next != NULL) && (steps < 2)) { 16 next = next->next; 17 ++steps; 18 } 19 20 if (NULL == next) { 21 return NULL; 22 } 23 24 if (curr == next) { 25 break; 26 } 27 } 28 29 ListNode *next_head = head->next; 30 31 next = next->next; 32 33 while (curr != next) { 34 next = next->next; 35 next_head = next_head->next; 36 } 37 38 while (head != next_head) { 39 head = head->next; 40 next_head = next_head->next; 41 } 42 43 return head; 44 } 45 };
转载于:https://www.cnblogs.com/panda_lin/p/linked_list_cycle_ii.html
leetcode: Linked List Cycle II相关推荐
- LeetCode Linked List Cycle II(floyd cycle)
问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...
- [LeetCode] Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode Linked List Cycle II
题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null 思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始 ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- leetcode day5 -- Reorder List Linked List Cycle II
1. Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
最新文章
- ICML2020 | 神经网络的图结构如何影响其预测性能?
- java参数传递(超经典)
- matlab用diag直接使用错误_你真的用对了卫生巾吗?这6个错误的使用方法,你占了几个...
- 第九章 Shell信号发送与捕捉
- python画折线图代码-python绘制简单折线图代码示例
- 自动挂载ios_开机自动挂载iso
- Vue parse之 从template到astElement 源码详解
- 金额逾千万!浪潮智能存储G2中标华中科技大学脑科学研究项目
- Linux内存管理:NUMA技术详解(非一致内存访问架构)
- hadoop hdfs 集群配置
- oracle对象依赖关系图,Oracle concepts 学习笔记(4)——Schema对象间的依赖关系
- 【数据分析】脑图讲述数据分析方法论
- 掌握业务流程图符号,提高业绩不再没有头绪
- yum源修改为本地光盘镜像
- 视频演示-Snapper快捷优秀的音频预览播放器演示
- linux整盘封装生成iso,一键自安装ISO封装打包脚本 | 聂扬帆博客
- 关于《职场路上》专栏介绍
- 七种常见的数据分析法之:可行域分析
- systemd的日志存在哪里?
- day 86 Vue学习之五DIY脚手架、webpack使用、vue-cli的使用、element-ui
热门文章
- 【深度学习】深度学习之对抗样本问题和知识蒸馏技术
- 基础练习 十进制转十六进制
- html自动兼容像素密度,解决 HTML Canvas 元素在高像素密度/高分辨率屏幕上显示模糊的问题...
- java packetmaster_TCP中间件_java_server
- 需求分析的接口需求_再谈需求分析
- dell笔记本耳机怎么设置_win10笔记本怎么设置合上盖子不休眠
- 请概述可视化卷积神经网络的中间输出的基本思想。_卷积神经网络为什么能称霸计算机视觉领域?...
- 营销型网站吸引用户说难也难,说简单也简单
- 网页快照是什么?对SEO优化有什么作用?
- python调用libs.dbutil_Python 使用 PyMysql、DBUtils 创建连接池,提升性能