【题解】【链表】【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?
思路
这题是Linked List Cycle的进阶版
Given a linked list, determine if it has a cycle in it.
bool hasCycle(ListNode *head) {
if(head == NULL) return false; //带环链表还要考虑只有单个元素的情况
ListNode *faster = head, *slower = head;
while(faster->next != NULL && faster->next->next != NULL && slower->next != NULL){//直接判断faster->next->next != NULL会抛错
faster = faster->next->next;
slower = slower->next;
if(faster == slower)
return true;
}
return false;
}
faster和slower相遇之后必然在环上,让slower再走一圈计算环的长度len。另让两个指针p1,p2从head开始走,p1比p2先走len步,这样当p2走到环开始处时,正好p1与p2第一次相遇。
ListNode *detectCycle(ListNode *head) {
if(head == NULL) return NULL;
//带环链表要考虑只有单个元素的情况
ListNode *faster = head, *slower = head;
while(faster->next != NULL && faster->next->next != NULL && slower->next != NULL){//直接判断faster->next->next != NULL会抛错
faster = faster->next->next;
slower = slower->next;
if(faster == slower){
ListNode *p1 = head;//另让两个指针p1,p2从head开始走
ListNode *p2 = head;
slower = slower->next;//让slower再走一圈计算环的长度len
p1 = p1->next;//设len是环的长度,p1比p2先走len步
if(faster == slower) return faster;//自环
while(faster != slower){
slower = slower->next;
p1 = p1->next;
}
while(p1 != p2){//当p1与p2第一次相遇时,正好p2走到环开始处
p1 = p1->next;
p2 = p2->next;
}
return p2;
}
}
return NULL;
}
【题解】【链表】【Leetcode】Linked 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, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [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 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
- [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 ...
随机推荐
- NimBus一个好的开发框架
NimbusKit是一个非常适合有经验的开发人员使用的iOS开发框架,具备完整的文档,并且提供了模块化的方式来解决iOS开发中的各种不同需求.最重要的是,该框架会经常添加一些新的组件和功能. Nimb ...
- Assert断言测试
assert编写代码时,我们总是会做出一些假设,断言就是用于在代码中捕捉这些假设,可以将断言看作是异常处理的一种高级形式.断言表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真.可以在 ...
- sql语句查询重复的数据
查找所有重复标题的记录: SELECT *FROM t_info aWHERE ((SELECT COUNT(*)FROM t_infoWHERE Title = a.Title) > 1)OR ...
- Js笔试题之parseInt()和.map()
parseInt()的几个例子 var b = parseInt("01"); alert("b="+b); var c = parseInt("09 ...
- Halcon 10.0:Hobject图像转CBitmap
void HImage2CBitmap(Hobject pImage,CBitmap *wImage) { char lpcsType[MAX_STRING]; Hlong lPointer,widt ...
- java的动态绑定和静态绑定
首先是方法的参数是父类对象,传入子类对象是否可行然后引出Parent p = new Children();这句代码不是很理解,google的过程中引出向上转型要理解向上转型又引出了动态绑定从动态绑定 ...
- “System.Threading.ThreadAbortException”类型的第一次机会异常在 mscorlib.dll 中发
问题原因: Thread.Abort 方法 .NET Framework 4 其他版本 1(共 1)对本文的评价是有帮助 - 评价此主题 在调用此方法的线程上引发 ThreadAbortExce ...
- vs2012 断点不能调试
调试ASP.NET时发现,设置的断点被视而不见 提示错误 debugging information for ‘iisexpress.exe’cannot be found or does not m ...
- python tuple 操作
特点:不可改(与字符串一样.不允许删除和修改) 操作:1.print 可使用跟%的元祖 print( '%s is %d years old' % (name, age)) 2.像列表一样有索引 3 ...
- AS的快捷键
Ctrl+Shift+Alt+N 查找类中的方法或变量 Ctrl+P 方法参数提示 Alt+Insert 生成代码(如get,set方法,构造函数等) 删除导入多余的包Ctrl+Alt+o 提取局部变 ...