[LeetCode]Link 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?
思考:第一步:设环长为len,快慢指针q、p相遇时,q比p多走了k*len。
第二部:p先走k*len步,p,q一起走每次一步,相遇时p比q多走了一个环距离,此时q就是环开始结点。
通过分析AC的感觉真好!
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.
if(!head||!head->next) return NULL;
ListNode *p=head;int countP=0;
ListNode *q=p->next;int countQ=1;
bool flag=false;
int len=0;
while(q&&q->next)
{
if(p==q)
{
len=countQ-countP;
flag=true;
break;
}
else
{
q=q->next->next;
p=p->next;
countP+=1;
countQ+=2;
} }
if(flag)
{
p=head;
q=head;
while(len--)
{
p=p->next;
}
while(p!=q)
{
p=p->next;
q=q->next;
}
return p;
}
else return NULL;
}
};
[LeetCode]Link List Cycle II的更多相关文章
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [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解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- 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
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode]Link List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- 济南学习 Day 3 T1 am
NP(np)Time Limit:1000ms Memory Limit:64MB题目描述LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子的:有 ...
- C++ 单链表基本操作
链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...
- JS判断客户端是手机还是PC
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...
- 14.Apache配置
环境: ↗ atl.example.com (192.168.1.101) ↗ www.e ...
- html关于强制显示 隐藏浏览器的滚动条
浏览器的滚动条在一些特殊的展示中,是不需要的,所以必须把它隐藏掉,文章主要介绍一些隐藏或者显示IE的水平或者垂直滚动条的实现代码,需要了解的朋友可以参考下: 相关css代码如下: //强制显示滚动条: ...
- jquery插件formValidator的ajaxValidator传参数问题
最近在用formValidator插件,遇到一个问题.当我想用ajaxValidator的url传参数时,$("#tbName").val().document.getElemen ...
- Ubuntu下设置Tomcat成为服务(开机启动)
1.将tomcat安装目录下bin文件夹中的catalina.sh拷贝到/etc/init.d下并修改名称为tomcat cp /path/to/tomcat/bin/catalina.sh /et ...
- 删除所有表数据的sql语句
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' EXEC sp_MSForEachTable 'ALTER TABLE ? ...
- WINDOWS下更改MYSQL数据路径(datadir)后服务启动1067解决不能改变mysql数据库存储位置
晚上安装完MYSQL(系统:深度WINXPSP2, MYSQL版本:5.1.32)后,用MYSQL自带的配置工具配置完发现默认的数据存放路径是:C:/Documents and Settings/Al ...
- MemProof教程
简介 MemProof(内存清道夫)是AutomatedQA出品的一款非常不错的检测内存泄漏和资源泄漏的免费调试工具,适合于WIN32平台下使用DELPHI/C++ BUILDER开发的应用程序. 利 ...