Given a linked list, determine if it has a cycle in it.

Follow up: Can you solve it without using extra space?

思考:快慢指针,快指针一次走两步,慢指针一次一步。若快指针跟慢指针指向同一个结点,则有环。若快指针到达链表末尾即指向NULL,说明没有环。

/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(!head||!head->next) return false;
ListNode *p=head;
ListNode *q=p->next;
while(q&&q->next)
{
if(p==q) return true;
else
{
q=q->next->next;
p=p->next;
}
}
return false;
}
};

  

[LeetCode]Link List Cycle的更多相关文章

  1. [LeetCode]Link List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. [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 ...

  5. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  6. 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 ...

  7. LeetCode——Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. LeetCode Linked List Cycle 解答程序

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

  9. [算法][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 ...

随机推荐

  1. cass实体编码列表

    地物名称 编码 图层 类别 参数一 参数二 实体类型 三角点 131100 KZD 20 gc113 3 SPECIAL,1 三角点分数线 131110 KZD 附 LINE 三角点高程注记 1311 ...

  2. debian终端菱形乱码修复

    最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok

  3. C89 和 C99的标准比较

    本文转载自: C89和C99标准比较  原文转载自: http://blog.programfan.com/article.asp?id=14051  http://blog.csdn.net/xgb ...

  4. Low-poly低面建模(低像素多边形)

    概念 继拟物化.扁平化(Flat Design).长阴影(Long Shadow)之后,低多边形(Low Poly)又火速掀起了最新设计风潮.这种设计风格在早期计算机建模和动效中就被广泛采用,在快要被 ...

  5. Windows phone 中一些实用的控件

    一.TextBlock:这个控件其实就是Label控件. <TextBlock x:Name="PageTitle" Text="page name" M ...

  6. WP开发笔记——去除 HTML 标签

    获取到一段HTML类型的信息,显示在WP的webbrowser控件中,如果不加处理的话,会显示出各种神烦的HTML标签. 这时,需要我们将这HTML类型的信息进行处理去除HTML标签后再显示出来,这里 ...

  7. Android Studio添加jar包

    1.先把jar包复制到项目的lib下,

  8. ORACLE 语句关联统计

    很久不用SQL语句了,貌似入职新公司后,又回归到了三年前的SQL时代,一写一坨的SQL好吧,也当回归一下过去的知识. 下面是统计2月份某数据的计费统计 select t.telno as 主号,VID ...

  9. Winform webBrowser 不跳转网页

    private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...

  10. js定时器 特定时间执行某段程序的例子

    定时器想必大家并不陌生吧,在本文为大家详细介绍下js中是如何实现定时器的,具体原理及代码如下. 例子: $(function(){ var handler = function(){ //www.jb ...