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

 
Example

Given -21->10->4->5, tail connects to node index 1, return true

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

解法一:

 class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: True if it has a cycle, or false
*/
bool hasCycle(ListNode *head) {
ListNode * fast, * slow;
if (head == NULL) {
return false;
}
slow = head;
fast = head->next; while (fast != NULL && fast->next != NULL) {
if (slow == fast) {
return true;
}
slow = slow->next;
fast = fast->next->next;
} return false;
}
};

102. Linked List Cycle【medium】的更多相关文章

  1. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  2. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  3. 141. Linked List Cycle【Easy】【判断链表是否存在环】

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  4. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  5. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  8. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  9. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

随机推荐

  1. nib 加载过程分析以及对File’s Owner的理解

    nib loading的过程,这个是app文档里面有说到资源编程指南 1.  It loads the contents of the nib file and any referenced reso ...

  2. java中的char占几个字节

    1:“字节”是byte,“位”是bit : 2: 1 byte = 8 bit : char 在Java中是2个字节.java采用unicode,2个字节(16位)来表示一个字符. 例子代码如下: p ...

  3. Java:Object类详解

    Java的一些特性会让初学者感到困惑,但在有经验的开发者眼中,却是合情合理的.例如,新手可能不会理解Object类.这篇文章分成三个部分讲跟Object类及其方法有关的问题. 上帝类 问:什么是Obj ...

  4. Orchard运用 - 为博客启用Markdown编辑器

    有时决定你是否使用某一个博客系统,最看重就是如何更简便的写博客,不能让其成为一个负担或别扭费力不讨好的工作. 对此一个好的编辑器就是一个最靓丽的卖点.比如最新的博客系统ghost.org就只定位一个最 ...

  5. [置顶] OpenJDK源码研究笔记(九)-可恨却又可亲的的异常(NullPointerException)

    可恨的异常 程序开发过程中,最讨厌异常了. 异常代表着程序出了问题,一旦出现,控制台会出现一屏又一屏的堆栈错误信息. 看着就让人心烦. 对于一个新人来讲,遇到异常经常会压力大,手忙脚乱,心生畏惧. 可 ...

  6. css背景颜色渐变

    1.效果 2.代码 /* 基本色 */ background: #3FB0AC; /* chrome 2+, safari 4+; multiple color stops */ background ...

  7. 搭建SpringBoot服务器,在公司内网中使用

    搭建SpringBoot服务器,在公司内网中使用. 学习了:https://blog.csdn.net/z3881006/article/details/78902231 就是一个程序,托管于gith ...

  8. springMVC之增删改查

    一.核心原理 1. 用于发送请求给server: /home.htm 2. 请求被DispatchServlet拦截到 3. DispatchServlet通过HandleMapping检查url有没 ...

  9. Codeforces Round #250 (Div. 1) D. The Child and Sequence

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  10. C-IDE使用指南

    HI  您好: 亲爱的学员,本文章是基于C-IDE的入口使用指南,您能够查看demo项目来了解C-IDE详细操作哦~ 如有疑问您可提交反馈来咨询,或扫描下方二维码增加官方微信群.我们会认真对待且具体回 ...