141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false
思路:两个指针,一快一慢,能相遇则有环,为空了没环
ps:很多链表的题目:都可以采用这种思路
/**
* 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==NULL)return false;
ListNode *p1,*p2;
p1=p2=head;
while(p2->next&&p2->next->next){ //要判断p->next->next为空先要判断p->next是否为空,以免产生p->next->next不存在的蛋疼问题
p1=p1->next;
p2=p2->next->next;
if(p1==p2)
return true;
}
return false;
}
};
141 Linked List Cycle(判断链表是否有环Medium)的更多相关文章
- LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [leetcode]141. Linked List Cycle判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
随机推荐
- 【转】Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
原文网址:http://www.cnblogs.com/skywang12345/p/3308556.html 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具 ...
- (转载)eclipse 快捷键大全,eclipse查找类,文件,添加注释
(转载)http://hi.baidu.com/fegro/item/8224c8c28b174627ee466598 /* ----------------------------------- ...
- 动态规划(斜率优化):BZOJ 3675 [Apio2014]序列分割
Description 小H最近迷上了一个分割序列的游戏.在这个游戏里,小H需要将一个长度为N的非负整数序列分割成k+l个非空的子序列.为了得到k+l个子序列, 小H将重复进行七次以下的步骤: 1.小 ...
- 【单调栈】Vijos P1926 紫色的手链
题目链接: https://vijos.org/p/1926 题目大意: 给n个数(n<=100 000),求任意区间的最大值异或次大值的最大值. 题目思路: [模拟][单调栈] 我们维护一个严 ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- [转载]10 Best Tools For Websites And Apps Development Ever
转载自: http://www.websurfmedia.com/10-best-tools-for-websites-and-apps-development-ever/ The world i ...
- Android Market 分析【安卓市场】
安卓市场: 通过对表的分析,“下载任务”的数据来源于数据库[app_download],“已安装”的数据来源于数据库[software_installed]. 数据分析:----- bash-3.2# ...
- ios 打电话结束返回到应用中
在我们做打电话这个功能时,我们常常是调用这个方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel ...
- 辛星浅析跨域传输的CORS解决方式
首先我们有一个概念.那就是"同源准则",也就是same-origin policy,它要求一个站点(协议+主机+port号)来确定的脚本.XMLHttpRequest和Webso ...
- raknet unity3d
Raknet是一高性能的跨平台的网络库. 他主要基于UDP实现,性能非常好,能够做server. 鉴于unity3d较差的网络支持. 本人成功实现了raknet c# for unity3d的使用,s ...