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. boa cgi程序cgi_header: unable to find LFLF

    ftp必须用二进制模式上传才可以 sqlite3 arm-linux-gcc hello.c -o hello.cgi -I /cgi/include -L /cgi/lib -static -lsq ...

  2. SQL Alias

    There are two types of aliases that are used most frequently in SQL command: which is column alias a ...

  3. PHP中多IP段权限控制方案

    在某些项目中我们可能会用到根据IP段进行权限校验,比如不在我们配置的IP段内的用户访问某些页面或功能模块时,将提示其权限不够并禁止访问该页面的内容.鉴于项目中需求各异,下面只说下大致思路以及我个人的实 ...

  4. linux 基本命令学习

    原文: https://www.oschina.net/translate/useful-linux-commands-for-newbies?lang=chs&p=2

  5. linux 的命令 -exec 的使用

    linux中的 exec命令,-exec 后面跟的是linux的 command 命令,exec命令以分号结束‘:’, 该分号前面要放 反斜杠转义 . find . -name jquery.js - ...

  6. Virtualbox中Linux添加新磁盘并创建分区

    原文:https://www.linuxidc.com/Linux/2017-01/139616.htm ----------------------------------------------- ...

  7. jQuery框架开发一个最简单的幻灯效果

    在线演示 在这个课程中,我们将介绍如何使用jQuery来开发一个最简单的图片幻灯效果. 立刻观看互动课程:jQuery框架开发一个最简单的幻灯效果 阅读原文:jQuery框架开发一个最简单的幻灯效果

  8. 算法笔记_129:计数排序(Java)

    目录 1 问题描述 2 解决方案 2.1比较计数排序 2.2 分布计数排序   1 问题描述 给定一组数据,请使用计数排序,得到这组数据从小到大的排序序列. 2 解决方案 2.1比较计数排序 下面算法 ...

  9. SyntaxError: Non-UTF-8 code starting with '\xc5' in file t.py on line 3,but no encoding declared;see http://python.org/dev/peps/pep-0263/ for details

    解决方案是: 在程序最上面加上:# coding=gbk 这样程序就可以正常运行了.

  10. MVC中cshtml文件中怎样使用ViewBag进行筛选?

    @foreach (PermissionForRoleModel item in ((List<PermissionForRoleModel>)ViewBag.PermissionsFor ...