LeetCode-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?
首先确定是否有环,并找到环上一点,通过该点得到环的长度,用两个差为环长度的指针来探测环的起点。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode *detectCycle(ListNode *head) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.ListNode * p1=head,*p2=head;ListNode * pic,*ptr;if(head==NULL)return NULL;int length=1;//length of the circlewhile(true){p1=p1->next;if(p1==NULL)return NULL;p2=p2->next;if(p2==NULL)return NULL;p2=p2->next;if(p2==NULL)return NULL;if(p1==p2){pic=p1;ptr=p2;break;}}while(ptr->next!=pic){length++;ptr=ptr->next;}if(length==1)return pic;p1=head;for(int i=0;i<length;i++){p1=p1->next;}p2=head;while(p1!=p2){p1=p1->next;p2=p2->next;}return p1;} };
转载于:https://www.cnblogs.com/superzrx/p/3420817.html
LeetCode-Linked List Cycle II相关推荐
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- 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 ...
最新文章
- NOIP2013pj车站分级[拓扑排序]
- C语言中函数如何返回结构体?
- PHP中header的用法
- [html] Form表单是怎么上传文件的?你了解它的原理吗?
- fastdfs-客户端配置
- .Net Core控制台应用加载读取Json配置文件
- get_free_page 和其友
- aac蓝牙编解码协议_蓝牙协议总结
- 视觉感知「挑战」天花板,多摄像头环绕方案同比增长近100%
- Prescan:关于Prescan与Matlab联合仿真问题小总(不定时补充)
- KVASER 与 Matlab联合使用
- 华为云的云计算比阿里云的云计算认证好吗?
- Stata:工具变量法(两阶段最小二乘法2SLS)——解决模型内生性
- Ps光速制作文字矢量图
- 【diannaoxitong】word打不开或发送错误解决办法
- 人力资源管理专业知识与实务(初级)【2】
- 好书推荐《月亮与六便士》
- 利用正则表达式爬取豆瓣读书top250书籍信息 附有详细分析
- 数字电路反相器符号_反相器
- CS231n公开课系列1_视频+课件+作业下载+GitHub批量下载工具
热门文章
- python exceptions怎么用_Python基础介绍 | Exceptions异常
- 一加到1亿。C语言_一加官方道歉!这下良心了:老用户欢呼
- springboot http status 404 – not found_使用枚举简单封装一个优雅的 Spring Boot 全局异常处理!...
- 滴滴滴,ITester软件测试小栈所有资源放送!
- OpenSea2月总交易额为9390.4万美元 用户总数突破5万人
- SAP License:SaaS的生门到底在哪里?
- SAP License:如何导入License
- WEB前后端交互原型通用元件库、常用组件、信息输出、信息输入、信息反馈、综合系列、页面交互、首页、分类页、内容详情、用户中心、注册登录、找回密码、元件库、web元件库、rplib、axure
- Vue中Object和Array数据变化侦测原理
- 团队作业(二):项目选题