[LeetCode141]Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it.
判断一个链表是否有环
代码:
/**
* 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) {
ListNode *fast(head), *slow(head);
while(fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
if(fast == slow)
return true;
}
return false;
}
};
关于进一步求环的长度和入口结点可以看另一篇博客http://www.cnblogs.com/zhangbaochong/p/5151986.html
[LeetCode141]Linked List Cycle的更多相关文章
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 每天一道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 ...
- LeetCode141:Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- [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 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 15. Linked List Cycle && Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
随机推荐
- Linux内核升级
一.测试环境 CentOS6.5 X86 64位 内核版本为 2.6.32 VM 10.07 二.编译内核版本 2.1.kernel 3.2.71 2.2.kernel 3.4.108 2.3.ker ...
- 最经常使用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?优点在哪里? 3. C++对象序列化的四种方法 4. 最经常使用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序猿在编写应用程序的时候往往须 ...
- Android-->发送短信页面实现(短信发送以及群发和从电话本中选择联系人)-----------》2
分析下怎么写 首先,我们需要一个输入框,可以手动的输入手机号码, 其次,很少有人愿意手动输入,那么我们需要提供一个按钮来给我们的用户选择自己电话本中的联系人(一次可以选择多个即群发) 然后,我们需要一 ...
- Bootstrap之表格
基本实例 为随意<table>标签加入.table类能够为其赋予主要的样式-少量的内补(padding)和水平方向的分隔线. <table class="table&quo ...
- cocos2d-x ios游戏开发初认识(五) CCsprite精灵类
这次写一下精灵创建的几种类型: 一.通过文件创建: 在原有的基础上加入例如以下代码: //一.通过文件创建精灵 CCSprite *bg =CCSprite::create("map.png ...
- 使用GraceNote Web API发展Mac发现音乐信息的应用
好久没有写博客,最近各种忙,特别忙里忙,今晚难得清闲.写最近完成下一个博客任务的摘要:使用GraceNote的Web API开发一个查询的音乐信息的应用,事实上,并在这些功能的前GraceNote S ...
- Codeforces 191 C Fools and Roads (树链拆分)
主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...
- 判断 iPhone 是否已插入 SIM 卡的方法
判断 iPhone 是否插入了 SIM 卡,可以参考苹果官网的 systemconfigure framework 教程,将下面的代码复制到头文件 extern NSString* const kCT ...
- ECshop lib_base.php on line 1241 错误解决方法
ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...
- 使用C++名单在文档处理和学生成绩管理系统相结合
对于学生成绩管理系统,我并不陌生,几乎学习C人的语言.做项目会想到学生成绩管理系统,我也不例外.在研究中的一段时间C语言之后,还用C语言到学生管理系统,然后做几个链接.计数,这个系统是以前的系统上的改 ...