[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 ...
随机推荐
- 4_1 wp8数据绑定与独立存储空间[wp8特色开发与编程技巧]
Wp8数据绑定与独立存储空间 数据绑定为基于 Silverlight 的应用程序提供了一种显示数据并与数据进行交互的简便方法. 数据的显示方式独立于数据的管理. UI 和数据对象之间的连接或绑定使数据 ...
- <hash命令:显示、添加或清除哈希表>
linux系统下的hash指令: 说明:linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样.第一次执行命令s ...
- 基于WORDPRESS+MYSQL的绿色企业主题制作全过程
基于WORDPRESS+MYSQL的绿色企业主题制作全过程基于WORDPRESS+MYSQL的绿色企业主题制作全过程基于WORDPRESS+MYSQL的绿色企业主题制作全过程基于WORDPRESS+M ...
- 【原】WinForm中的DataGridView加入Combbox或者DropDownButton后,操作变慢
DataGridView里加入了DropDownButto列,加载数据后点击这一列,反应很慢;要点击三到四次才会展示下拉列表; 原因是DataGridView的EditMode设置问题; 将DataG ...
- 所有外包项目威客网站列表----来自程序员接私活网qxj.me
猪八戒 http://www.zhubajie.com/ 有佣金,建议别去坑死了 csto http://www.csto.com/ 开源中国众包 https://zb.osch ...
- java 环境变量设置
JAVA_HOME C:\Program Files\Java\jdk1.7.0 PATH %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; CLASSPATH .;%JAV ...
- Oracle varchar2 4000
关于oracle varchar2 官方文档的描述 VARCHAR2 Data Type The VARCHAR2 data type specifies a variable-length char ...
- Is C# a clone of a Microsoft replacement for Java?
Is C# a clone of a Microsoft replacement for Java?Let's look at what Anders Hejlsberg Said. Hejlsber ...
- 通过数据绑定模板得到对应的Item控件
这类控件都继承于Selector,其中主要有ComboBox.listview.listbox.datagrid. 由于个人对WPF的了解所有可能有遗漏,希望各位能够指出一起进步. 在遍历上面控件时主 ...
- 【html5】这些新类型 能提高生产力
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...