【LeetCode】141. Linked List Cycle (2 solutions)
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)的更多相关文章
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【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 ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【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 ...
- 【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 聊聊并发(四)——深入分析ConcurrentHashMap
线程不安全的HashMap 因为多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap,如以下代码 final HashM ...
- Android Protobuf应用及原理
前言 之前一直忙于移动端日志SDK Trojan的开源工作,已十分稳定地运行在饿了么团队App中,集成了日志加密和解密功能.哎呀,允许我卖个狗皮膏药,不用不知道,用了就知道,从此爱不释手,Trojan ...
- P值(P-value),“差异具有显著性”和“具有显著差异”
郑冰刚提到P值,说P值的定义(着重号是笔者加的,英文是从WikiPedia摘来的): P值就是当原假设为真时,比所得到的样本观察结果更极端的结果出现的概率. The P-value is the pr ...
- Java并发学习之十五——使用读写锁同步数据訪问
本文是学习网络上的文章时的总结.感谢大家无私的分享. 读写锁重要的是写锁的使用,仅仅用一个入口. 以下是读写锁使用的样例 package chapter2; import java.util.conc ...
- 聊聊clean code
clean code,顾名思义就是整洁的代码,或者说清晰.漂亮的代码,相信大多数工程师都希望自己能写出这样的代码. 也许这是个千人千面的话题,每个工程师都有自己的理解.比如我,从一个天天被骂代码写得烂 ...
- iOS:UIView视图与组件控件
一.UIView常见属性 (1)@property(nonatomic,readonly)UIView *superview; //获取自己的父控件对象 (2)@property(nonatomi ...
- guess-number-higher-or-lower
// Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...
- CREATE DATABASE failed
由于环境需要, 故修改SQL Server 2012的默认的数据库的数据文件和日志文件的位置. 如下: 创建数据库, 遭遇报错. 错误信息: A file activation error occur ...
- Java中浮点类型的精度问题 double float
要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法与浮点数的结构组成.因为机器只认识01,你想表示小数,你要机器认识小数点这个东西,必须采用某种方法.比如,简单点的,float四个字 ...
- MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.
最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...