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. 把Java数组转换为List时的注意事项

    本文由 ImportNew - 飘扬叶 翻译自 mlangc.欢迎加入翻译小组.转载请见文末要求. 不幸的是并不是每件事都尽如人意.举个例子,现在将一个Java数组转换为List.当然,我们可以使用A ...

  2. ChartView与LineSeries搭配实现曲线局部缩放功能

    效果图: 上一篇文章实现的时候还不知道有QtChart这个模块......好好看了下资料就想做个例子实现一下这功能,比较了下代码量...恩,直接看代码: Rectangle { id: view_re ...

  3. 利用FPGA实现PCI总线接口及Windows驱动实现

    利用FPGA实现PCI总线接口及Windows驱动实现 关于PCI总线协议,资料网上.书本都是.这里我们仅仅对重点对利用FPGA实现PCI总线接口问题进行简单分析.下图是PCI总线接口信号: 配置空间 ...

  4. js 获取url的get传值函数

    function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s| ...

  5. magento 12 配置安装教程

    Magento (麦进斗) 是一套专业开源的电子商务系统.Magento设计得非常灵活,具有模块化架构体系和丰富的功能.易于与第三方应用系统无缝集成.其面向企业级应用,可处理各方面的需求,以及建设一个 ...

  6. 很全的Python 面试题 github

    https://github.com/taizilongxu/interview_python

  7. iOS kvo 结合 FBKVOController 的使用

    iOS kvo 结合 FBKVOController 的使用 一:FBKVOControlloer是FaceBook开源的一个 在 iOS,maxOS上使用 kvo的 开源库: 提供了block和@s ...

  8. es5 - array - unshift

    /** * 描述:该unshift()方法从数组中添加单个或多个元素,并且返回长度 * 语法:arr.unshift(element1 [,... [,elementN ]]) * 参数:要添加到数组 ...

  9. redis配置密码的方法

    打开redis.conf配置文件,找到requirepass,然后修改如下: requirepass yourpasswordyourpassword就是redis验证密码,设置密码以后发现可以登陆, ...

  10. OSI模型图文说明

    网关工作在第四层传输层及其以上 网络层:路由器 数据链路层:网桥,交换机 物理层:网卡,网线,集线器,中继器,调制解调器 OSI共7层:应用层,表示层,会话层,传输层,数据链路层,物理层. [7]:应 ...