Description

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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Challenge

Follow up:
Can you solve it without using extra space? (O(1) (i.e. constant) memory)?

Solution

 /**
* 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) {
if(head==NULL) return false; ListNode *fast = head;
ListNode *slow = head; while(true){
// If a node is NULL, there is no cycle.
if(fast->next == NULL || fast->next->next == NULL) return false; slow = slow->next;
fast = fast->next->next; // When the fast and the slow run into the same node, there's a cycle.
if ( slow->val == fast->val )
return true;
}
}
};

[Algorithm] 11. Linked List Cycle的更多相关文章

  1. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

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

  2. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  3. Leetcode 中Linked List Cycle 一类问题

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

  4. LeetCode: Linked List Cycle II 解题报告

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

  5. LeetCode: Linked List Cycle 解题报告

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

  6. [LeetCode] Linked List Cycle II, Solution

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

  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 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  9. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

随机推荐

  1. beego1---beego,bee环境配置

    1.配置环境变量GOPATH(代码路径,先在里面建立src,pkg,bin3个目录),GOROOT:go安装的目录,go安装目录下的bin目录放到Path环境变量. 安装完bee工具之后,bee 可执 ...

  2. 蓝牙驱动分析 linux

    蓝牙驱动分析 这个驱动分析的是OK6410开发板自带的内核版本是linux3.0.1,所支持的wifi和蓝牙一体芯片是marvell的8688和8787.根据开发板的设计,芯片与主机之间是通过sdio ...

  3. jQuery EasyUI,LinkButton(按钮)组件

    转自:https://www.cnblogs.com/adc8868/p/6639570.html jQuery EasyUI,LinkButton(按钮)组件 学习要点: 1.加载方式 2.属性列表 ...

  4. kernel信息及其相关命令

    内核 linux内核是单内核体系设计.但充分借鉴了微内核设计体系的优点,为内核引入模块化机制 内核组成部分: kernel: 内核核心,一般为bzImage,通常在/boot 目录下,名称为vmlin ...

  5. java虚拟机全集(31篇文章)

    深入理解java虚拟机系列 深入理解Java虚拟机笔记---内存区域 深入理解Java虚拟机笔记---判断对象是否存活 深入理解Java虚拟机笔记---垃圾收集算法 深入理解Java虚拟机笔记---垃 ...

  6. 源码阅读之HashMap(JDK8)

    概述 HashMap根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap最多只允许一条记录的键为null,允许多条记录 ...

  7. 基于.Net Core的API框架的搭建(2)

    4.加入数据库支持 下面我们为项目加入数据库支持,修改appsettings.json: 然后我们要生成实体类,打开VS工具->NuGet包管理器->程序包管理器控制台: 输入命令: Sc ...

  8. 在Chrome与火狐中,输入框input类型为number时,如何去除掉的自带的上下默认箭头

    如何移除input='number'时浏览器自带的上下箭头: CSS样式: /* 去除input[type=number]浏览器默认的icon显示 */ input::-webkit-outer-sp ...

  9. hdu5922Minimum’s Revenge

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  10. Tomcat6和7版本对web.xml中taglib标签的配置差异

    原来部署在Tomcat6中的应用在Tomcat7中运行时报错如下错误: java.lang.IllegalArgumentException: taglib definition not consis ...