Linked List Cycle

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

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

解法一:

使用unordered_map记录当前节点是否被访问过,如访问过说明有环,如到达尾部说明无环。

/**
* 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) {
unordered_map<ListNode*, bool> visited;
while(head != NULL)
{
if(visited[head] == true)
return true;
visited[head] = true;
head = head->next;
}
return false;
}
};

解法二:不使用额外空间

设置快慢指针,

fast每次前进两步,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) {
ListNode* fast = head;
ListNode* slow = head;
do
{
if(fast != NULL)
fast = fast->next;
else
return false;
if(fast != NULL)
fast = fast->next;
else
return false;
slow = slow->next;
}while(fast != slow);
return true;
}
};

【LeetCode】141. Linked List Cycle (2 solutions)的更多相关文章

  1. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  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 ...

  3. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  4. 【LeetCode】142. Linked List Cycle II (2 solutions)

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

  5. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...

  7. 【easy】141. Linked List Cycle

    非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...

  8. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

  9. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. HBase目录

    1. HBase介绍及简易安装(转) 2. java操作Hbase实例 3. HBase入门基础教程之单机模式与伪分布式模式安装(转) 4. HBase教程 5. 用Java操纵HBase数据库(新建 ...

  2. go语言基础之结构体成员的使用普通变量

    1.结构体成员的使用普通变量 示例: package main //必须有个main包 import "fmt" //定义一个结构体类型 type Student struct { ...

  3. 取消Eclipse SVN的自动链接方式

    1. 选中指定的项目名(有文件夹样子的那个) 2. 右键,在在弹出菜单选择 Team 3. 然后再点击, Disconnect 即可.

  4. Deal with relational data using libFM with blocks

    原文:https://thierrysilbermann.wordpress.com/2015/09/17/deal-with-relational-data-using-libfm-with-blo ...

  5. Mongo的安全验证

    参考如下的文档: https://docs.mongodb.org/manual/tutorial/enable-authentication/          1.1. 在启用匿名验证的情况下,创 ...

  6. idea 本地调用zookeeper配置

  7. Python中的乱码

        我把写好的Python脚本导入到ArcGIS中的ToolBox中,在本机测试是没有问题的.为了把工具分享给其他人,即在其他电脑上使用,我必须将脚本文件(*.py)导入到工具箱文件(*.tbx) ...

  8. CAD批量合并文件

    要求:将整饰完成504幅单独的宗地图合并成一张总图,合并后,去掉其他要素,只保留毕合的权属线. 解决: 1.合并dwg文件,除了手工粘贴复制外,最先想到的是插入块,即用Insert命令插入,测试结果可 ...

  9. UML 之 数据流图(DFD)

          数据流图(Data Flow Diagram):简称DFD,它从数据传递和加工角度,以图形方式来表达系统的逻辑功能.数据在系统内部的逻辑流向和逻辑变换过程,是结构化系统分析方法的主要表达工 ...

  10. Inside GDALAllRegister之四: 跳过driver

    这个函数很短小: /** * \brief This method unload undesirable drivers. * * All drivers specified in the space ...