LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)
来源:互联网 发布:apache 配置basic认证 编辑:程序博客网 时间:2024/06/06 19:23
判断cycle存在
Given a linked list, determine if it has a cycle in it.
Follow up: Can you solve it without using extra space?
本题要求判断给定链表中是否存在循环。并且题目要求不要使用extra space,因此,我们就不能保存每一个结点的value,在遍历的时候判断是否是循环。这道题可以通过使用速度不同的pointer进行遍历,若两个pointer相遇则说明存在cycle,若遇到NULL指针,则不存在cycle。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { if (!head) return false; ListNode* l1 = head; ListNode* l2 = head; while(true) { if (!l1 || !l2) return false; l1 = l1->next; if (!l1) return false; l1 = l1->next; l2= l2->next; if (l1 == l2) return true; } }};
寻找cycle入口结点
iven 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?
本题在上一题的基础上,进一步要求我们给出循环的入口结点,如下图中入口结点为3.
由第一题我们可以判断链表是否存在循环,若存在循环,两个pointer会在结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *detectCycle(ListNode *head) { if (!head) return false; ListNode* l1 = head; ListNode* l2 = head; while(true) { if (!l1 || !l2) return NULL; l1 = l1->next; if (!l1) return NULL; l1 = l1->next; l2= l2->next; if (l1 == l2) break; } l1 = head; while (l1 != l2) { l1 = l1->next; l2 = l2->next; } return l1; }};
解法证明
相信运行过上面代码的同学可以发现以上解法确实是正确的,但是这个解法看起来并不是很直观,为了打消同学们的疑虑,下面我将证明上面解法的正确性。
如上图所示的链表,循环外长度设为
假设有两个指针分别为
l1=head,v1=1 l2=head,v2=2
假设两个指针相遇的位置为
这里,由
也就是说如果存在循环的话,两个指针肯定会相遇,且相遇时指针前进的步数等于循环列表的长度。使用上面的图中的例子,我们可以看出,
到此我们得到了判断循环存在的证明结果,下面证明获取循环入口结点的解法。
在第一题中两个指针相遇时,前进的步长为循环的长度
但是根据上面的思路我们存在一个问题,我们并不知道
- LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)
- Linked List Cycle II 找链表环入口 @LeetCode
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode: Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode] - Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode]Linked List Cycle
- 【LeetCode】Linked List Cycle
- Linked List Cycle - LeetCode
- windows下使用git拉取源码文件编码问题
- 网页加载进度页面
- Eclipse快速输入代码 Java初学者必看
- iphone, ipad运行命令
- 选择相信黑马,相信自己,加油!!
- LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)
- mybatis配置文件之resultMap和resultType
- arcgis for android 中本地数据渲染重置的问题
- WINBASEAPI 与WINAPI(__stdcall __cdecl)的区别
- UC浏览器 分享到朋友圈和微信好友
- JSP中<img>标签图片在浏览器中不显示问题
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端
- Java基础--网络编程之UDP
- css学习笔记——CSS 伪类