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. MySQL高可用之MHA (转)

    MySQL高可用之MHA MHA简介 MHA是由日本人yoshinorim(原就职于DeNA现就职于FaceBook)开发的比较成熟的MySQL高可用方案.MHA能够在30秒内实现故障切换,并能在故障 ...

  2. C# 网卡IP(网上资料整理)

    //设置对外访问所使用网卡的IP string sendingIp = "192.168.0.1"; //设置对外访问所使用的端口 ; Uri uri = new Uri(&quo ...

  3. ajax对一些没有接口的数据进行分析和添加方法

    对于一些没有接口的数据进行分析和添加方法: <script src="ajax.js"><script>//插入ajax文件 <script> ...

  4. RHEL安装docker-compose

    Note that Compose 1.5.2 requires Docker 1.7.1 or later. pip install docker-compose==1.5.2 Note that ...

  5. Android SDK 更新失败

    万恶的墙,调查兵团赶紧把墙拆了.大家一起跟巨人打一架. 解决方法是改hosts文件 添加 74.125.237.1 dl-ssl.google.com ok,good job 多亏了http://bl ...

  6. res/raw文件的存放和读取

    通常,如果Android开发者有些文件比如音频,视频,.html,.mp3等等这些文件不希望编译器编译而保持原始原貌打包进apk文件(这在游戏开发中很常见和普遍,如游戏用到的游戏音乐.图等资源),那么 ...

  7. 【转】Python的XML-RPC简介

    编写客户端提交数据到服务器处理是程序员最常碰到的几个问题之一.各种不同的语言对此都有相应的解决方案.比如Unix下,C程序员们可以用SUNRPC,Java程序员则使用RMI来处理.大多数语言还都可以使 ...

  8. Jackson怎样转换这样的字符串? String jsonStr = "{dataType:'Custom',regexp:'t\\d+',msg:'输入不正确'}";

    字符串 String jsonStr = "{dataType:'Custom',regexp:'t\\d+',msg:'输入不正确'}"; 实体 package com.asia ...

  9. MySQL 多实例启动和关闭脚本

    DB: 5.5.14 OS:CentOS 6.3 在MySQL多实例中,一个一个启动灰常麻烦,因此写个简单的启动和关闭脚本 启动脚本:start.sh #!/bin/bash for port in ...

  10. 【http】生命周期和http管道技术 整理中

    httpModules 与 httpHandlers  正在写demo public class Httpext : IHttpModule { public void Dispose() { thr ...