leetCode刷题归纳-linked list(141. Linked List Cycle&& Add to List 142. Linked List Cycle II)
来源:互联网 发布:apk软件官方下载 编辑:程序博客网 时间:2024/05/16 23:36
这一题还蛮有意思的,虽然不太复杂,但是也是一种很简便的算法思想,所以在此做一下记录
题目描述: 141. Linked List Cycle I
Given a linked list, determine if it has a cycle in it.
Follow up: Can you solve it without using extra space?
Subscribe to see which companies asked this question.
解题思想
难点就在于不让消耗空间复杂度,这样怎么办?
我们可以模拟一个“赛跑”的过程:
/** * 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 *walk=head,*run=head->next; while(run&&run->next&&run->next->next){//将run->next放在前面避免发生NULL->next的情况 if(run==walk) return true; run=run->next->next;//run跳两步 walk=walk->next;//walk只走一步 } return false; }};
————————————————-我是分割线——————————————————
题目描述: 142. Linked List Cycle II
解题思想
难点就在于不让消耗空间复杂度,这样怎么办?
我们可以模拟一个“赛跑”的过程:
题目的要求改成了:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
现在的问题不是有没有环,而是环开始的位置在哪里,要求更高了,怎么办?
还是“赛跑”的方法,求出环的开始点的偏移量:
/** * 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 || !(head->next)) return NULL; ListNode *walk=head,*run=head; bool isCycle=false; while(run->next&&run->next->next){ run=run->next->next; walk=walk->next; if(walk==run) {isCycle=true; break;} } if(!isCycle) return NULL; //这里非常重要!好好记住这个性质!!! walk=head; while(walk!=run){ walk=walk->next; run=run->next; } return walk; }};
下面还有一种利用样例漏洞的办法,是一开始想出来的办法,比较起来有点low就一笔带过了。。。。需要解释的话可以留言。。。
阅读全文
0 0
- leetCode刷题归纳-linked list(141. Linked List Cycle&& Add to List 142. Linked List Cycle II)
- [LeetCode]141. Linked List Cycle【&142. Linked List Cycle II】
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- LeetCode 141. Linked List Cycle && 142. Linked List Cycle II
- LeetCode[Linked List]: Linked List Cycle II
- 142. Linked List Cycle II leetcode list
- 141. Linked List Cycle & 142. Linked List Cycle II
- 141. Linked List Cycle&142. Linked List Cycle II
- Linked List Cycle II Linked List Cycle
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- 烟大OJ.打印金字塔
- 常用<meta>元素整理与详解
- YTUOJ1907 杨辉三角
- Hdu1049
- CSS选择器
- leetCode刷题归纳-linked list(141. Linked List Cycle&& Add to List 142. Linked List Cycle II)
- 一. Volley简介 Volley的中文翻译为“齐射、并发”,是在2013年的Google大会上发布的一款Android平台网络通信库,具有网络请求的处理、小图片的异步加载和缓存等功能,能够帮助
- 微型技术博客-探究connect()函数。
- 英特尔i9或重燃核心大战 VR淡出电脑展
- 空理彷徨的诡异鱼跃之光
- 区块链开发专题(真正的区块链定义)
- static
- Tensorflow 学习速率的设置|学习速率的指数下降
- 计算机中有符号整数“循环”