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

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

注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判断元素和第一个元素是否存在重合

先设置两个指针p_fast和p_slow。从头开始遍历链表,p_fast每次走两个节点,而p_slow每次走一个节点,若存在循环,这两个指针必定重合:

 /**
* 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 *p_fast = head;
ListNode *p_slow = head; do{
p_slow = p_slow->next;
if(p_fast != NULL)
p_fast = p_fast->next;
if(p_fast != NULL)
p_fast = p_fast->next;
else
return false;
}while(p_fast != p_slow); return true; }
};

Linked List Cycle 判断一个链表是否存在回路(循环)的更多相关文章

  1. [算法][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 ...

  2. [leetcode]141. Linked List Cycle判断链表是否有环

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

  3. 141. 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 单链表中的环之二

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

  5. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  6. 力扣——Linked List Cycle(环形链表) python实现

    题目描述: 中文: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. ...

  7. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  8. [LC]141题 Linked List Cycle (环形链表)(链表)

    ①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...

  9. 20140719 找到单链表的倒数第K个节点 判断一个链表是否成为一个环形 反转

    1.找到单链表的倒数第K个节点 2.判断一个单链表对否形成环形 3.单链表翻转

随机推荐

  1. windows下python管理右键菜单

    实现很简单,不记得什么时候写的了,贴出来希望能有所价值 """ Windows中创建右键菜单 """ import os import sy ...

  2. [转] Java中Comparator进行对象排序

    [From] https://blog.51cto.com/thinklili/2063244 Java在8后引入了lambda表达式和流,使得排序方法有了变化 class User { int id ...

  3. Formatting HDFS

    Working on hadoop, especially on test clusters, I have managed to break my HDFS layer and sometimes ...

  4. Linux下的Shell特殊符号大全(转)

    在shell中常用的特殊符号罗列如下: # ; ;; . , / \ 'string'| ! $ ${} $? $$ $* "string"* ** ? : ^ $# $@ `co ...

  5. win10开启 linux Bash命令(win10内置了linux系统支持)

    win10开启 Ubuntu linux Bash命令(win10内置了linux系统支持) 第一步: 先在设置→更新和安全→针对开发人员中选择"开发人员模式",点击后会下载&qu ...

  6. java service wrapper将java程序包装成系统服务(一)

    一. 概述 使用java语言开发应用程序,在windows平台下,一般存在3种应用形式: 1. web应用.web应用多数打成war包在web容器(如tomcat,jetty等)中运行. 2. 桌面应 ...

  7. MySQL error2003错误原因以及解决方案

    转自:http://hi.baidu.com/tianxia339/item/8e8849111461ea7e7a5f2540 出现ERROR 2003 (HY000): Can't connect ...

  8. 数据库~Mysql里的Explain说明

    对于mysql的执行计划可以在select前添加Explain来实现,它可以告诉我们你的语句性能如何. 下面是对explain的具体说明,也都是官方的,以后进行参考. id SELECT识别符.这是S ...

  9. CentOS VNC

    CentOS Linux:1.需要安装的包:tigervnc,tigervnc-server 2.配置显示分辨率.桌面和用户:编辑 /etc/sysconfig/vncservers参考注释掉的最后两 ...

  10. [转]OPEN(SAP) UI5 学习入门系列之一:扫盲与热身(上)

    本文转自:http://www.cnblogs.com/qianmarv/p/4671394.html 1 扫盲 什么是SAP Fiori? 了解SAP UI5必须要从SAP Fiori开始,两者概念 ...