2014.1.13 21:43
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?
Solution:
This problem doesn't only ask you to check if there is a loop in the list, but also to find out where you enter the loop.
It is a bit tricky to solve this problem, if you are too accustomed to that "chasing method" that we mentioned in Linked List Cycle.
Since you only have a "next" pointer, what you can do is to check if $ptr or $ptr->next satisfy some specific property.
See the following linked list:
Node 2 is the entrance of the loop. Both node 1 and node 4 points to it. If you start traversing the list, you are sure to reach node 2 first and node 4 later. If there is a $ptr pointing to node 4, you reach $ptr->next before you reach $ptr,right? In a normal linked list, this will never happen. That's how we use abnormal condition to detect this abnormal node.
This problem doesn't use a chasing strategy with two pointers. But it requires one-by-one probing for all nodes until the right one is found, with each probing having O(n) time complexity. Therefore, the overall time complexity is O(n^2), space complexity is O(1).
Accepted code:
1 // 1CE, 2TLE, 1AC, foolish mistake.. 2 /** 3 * Definition for singly-linked list. 4 * struct ListNode { 5 * int val; 6 * ListNode *next; 7 * ListNode(int x) : val(x), next(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 ListNode *detectCycle(ListNode *head) { 13 // IMPORTANT: Please reset any member data you declared, as 14 // the same Solution instance will be reused for each test case. 15 if(head == nullptr){ 16 return nullptr; 17 } 18 19 ListNode *ptr, *res; 20 21 res = head; 22 while(res != nullptr){ 23 ptr = head; 24 // Forgot to check nullptr, added by the way later. 25 while(ptr != res->next && ptr != nullptr){ 26 if(ptr == res){ 27 // ptr reaches res first, not what we expect 28 break; 29 } 30 // 1TLE here, forgot to move forward... 31 // 1CE here, ';' is missing!!!! 32 ptr = ptr->next; 33 } 34 if(ptr == res->next){ 35 // $ptr reaches res->next first, that means $res->next is the start of the loop 36 // while $res is the end of the loop, thus $ptr = $res->next is the result we want 37 return ptr; 38 }else{ 39 // 1TLE here, forgot to move forward.. 40 res = res->next; 41 } 42 } 43 44 return nullptr; 45 } 46 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)