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. 简单工厂模式&策略模式-简介与区别

    不得不说,这两种模式真的很像. 相似点:都用到了面向对象的继承.多态.抽象,都拥有相似的结构. 不同点:工厂模式仅提供具体的实例对象,怎么使用这个对象是client的自由,策略模式client可以通过 ...

  2. GDB:从单线程调试到多线程调试(MFiX单步调试)

    GDB:从单线程调试到多线程调试 1. 裸跑GDB 1.1 安装GDB sudo apt-get install gdb 1.2 编译程序 由于需要调试,因此编译的时候需要添加-g编译参数: 1.3 ...

  3. 获得自己电脑的SSH公匙

    关于什么是SSH请点击此"www.Baidu.com”网站了解 我这里只说怎么获取属于自己电脑的SSH公匙 本人是Win10电脑 所以相对来说简单一点  点击win ->选择设置-&g ...

  4. Mac 10.12安装网站圆形设计工具Axure RP Pro 7

    下载: (链接: https://pan.baidu.com/s/1hsaTafi 密码: 855y)

  5. java io 处理流,BufferdInputStream、BufferedWriter、BufferedReader

    BufferdInputStream:读取helloworld.java helloworld.java: package io; public class HelloWorld { public s ...

  6. Redis-集群 - 分片

    Redis是一个基于内存的数据库,其不仅读写速度快,每秒可以执行大约110000的写操作,81000的读取操作,而且其支持存储字符串,哈希结构,链表,集合丰富的数据类型.所以得到很多开发者的青睐.加之 ...

  7. 十分有趣的this指向题

    var num=5;//9 =>++this.num=>10 var obj={ num:3, fn:function() { num = 9;//window.num=9 (functi ...

  8. AMQP技术术语

    什么是AMQP AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.基 ...

  9. "Calendars" and "DateFormats" should not be static

    参见:Call to method of static java.text.DateFormat not advisable?   级别:bug, multi-threading Not all cl ...

  10. Tomcat源码分析——Session管理分析(上)

    前言 对于广大java开发者而已,对于J2EE规范中的Session应该并不陌生,我们可以使用Session管理用户的会话信息,最常见的就是拿Session用来存放用户登录.身份.权限及状态等信息.对 ...