[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 ...
随机推荐
- HDU 2571
设 d[ i ][ j ] 表示走到坐标为(i, j)的格子时最大的幸运值.初始化:dp[i][j]的第0行和第0列初始化为负的无穷大... #include <stdio.h> #inc ...
- 【风马一族_git_github】使用Github搭建个人网站
个人网站 访问 https://用户名.github.io ( 风马一族的Github网站 ) 搭建步骤 1)创建个人站点-->新建仓库(注:仓库名必须是[用户名.github.io]) 2) ...
- php下载文件,添加响应头
//下载,添加响应头信息 header('Content-type:application/octet-stream'); header('Content-Disposition:attachment ...
- 记一次linux samba服务问题调试
linux下samba服务加入windows域控后,samba共享名与合法用户名不应一致,否则无法访问此共享.
- !!! FAILED BINDER TRANSACTION !!! TransactionTooLargeException
- ::): !!! FAILED BINDER TRANSACTION !!! xxxRecorder 运行40多分钟,崩溃,捕获日志 03-12 14:50:12.353: E/JavaBinde ...
- [转]浅析AD Exchange——RTB模式
在上一篇文章中,我们了解了程序化购买,并且知道程序化购买的最基本的一种RTB的竞价模式,了解了DSP.SSP.Ad Exchange等概念,不清楚的同学可以看看上一篇文章<程序化购买>. ...
- mongodb在ubuntu下的couldn‘t remove fs lock errno:9 Bad file descriptor的错误
按照官网上的安装方法: 在ubuntu系统下有可能出现如下错误: couldn't remove fs lock errno:9 Bad file descriptor 此时需要修改文件所有者 $ s ...
- jQuery select的操作代码
jQuery對select的操作的实际应用代码. //改變時的事件 复制代码代码如下: $("#testSelect").change(function(){ //事件發生 j ...
- 利用Unicode属性移除文本中的标点符号
原文:http://bbs.csdn.net/topics/270033191 摘抄: str = str.replaceAll("[\\pP‘’“”]", "&qu ...
- 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient
AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http但是Asyn ...