[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 ...
随机推荐
- 【VxWorks系列】任务间同步与通信之信号量
信号量是VxWorks提供的最常用,最快速的一种任务间通信机制.VxWorks中信号量有三种:二值信号量,互斥信号量,计数信号量.下面一一介绍这三种信号量的作用与区别. 信号量通常的作用就是是控制任务 ...
- atitit.高级编程语言的特性 and 未来趋势与进化.doc
atitit.高级编程语言的特性 and 未来趋势与进化.doc 1 编程语言的发展历程 1 1.1 编程语言的进化,起始发现背后的思想 :不论什么两个系统之间的复杂性,都能够通过加入一个抽象层要屏蔽 ...
- 百度地图AP1
百度地图API的用法 百度地图API演示样例 百度地图API学习总结 <1> <%@ Page Language="C#" Inherits="Syst ...
- .Net程序猿乐Android开发---(4)注册页面布局
接下来我们介绍的登陆页面布局,在本节中,我们看一下注册页面布局,页面布局大同小异,来一起熟悉下基本控件的使用方法. 效果图: 1.加入注冊页面 右键选中layout目录,加入注冊页面.例如以下图 点击 ...
- TreeSet排序
TreeSet的排序能够通过两种方法来实现: 1.通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比較器进行排序. 2. ...
- Android5.0L下因sensorservice crash导致systemserver重新启动的第二种场景分析
一.出问题的场景 1.Sensorservice线程正在处理compass sensor事件的过程中.检查了一次buffer的指针的有效性,并在稍后会传递到AKM获取数据的函数接口中使用 2.Sens ...
- Windows编程之非模态对话框
1 创建非模态对话框 <1> HWNDCreateDialog( HINSTANCE hInstance, // handle to module LPCTSTRlpTemplat ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- 深入理解Oracle RAC 12c 笔记
深入理解Oracle RAC 12c 跳转至: 导航. 搜索 文件夹 1 概述 2 集群件管理和故障诊断 3 执行实践 4 新特性 5 存储和ASM 6 应用设计上的问题 7 管理和调优一个复杂的RA ...
- 依赖注入(DI)
依赖注入(DI) IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用“好莱坞原则”是应用程序以被动的方式实现对流程的定制.我们可以采用若干设 ...