leetcode 141、Linked list cycle
一种方法是用set存储出现过的指针,重复出现时就是有环:
class Solution {
public:
bool hasCycle(ListNode *head) {
set<ListNode*> st;
ListNode *p = head;
while(p) {
if (st.find(p) != st.end())
return true;
st.insert(p);
p = p->next;
}
return false;
}
};
另一种方法就是快慢指针,快指针一次走两步,慢指针一次走一步。无环时快指针会走向空指针,有环时快指针会赶上慢指针。
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast, *slow;
fast = slow = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (fast == slow)
return true;
}
return false;
}
};
leetcode 141、Linked list cycle的更多相关文章
- [LeetCode] 141&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- leetcode 141 142. Linked List Cycle
题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- 【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练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- <LeetCode OJ> 141 / 142 Linked List Cycle(I / II)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- LeetCode算法题-Linked List Cycle(Java实现)
这是悦乐书的第176次更新,第178篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第35题(顺位题号是141).给定一个链表,确定它是否有一个循环. 本次解题使用的开发工 ...
随机推荐
- Luogu P1608 路径统计 最短路计数
颓了...重边导致我乖乖用邻接矩阵.... 好吧就是个最短路计数....如果更新时d[v]==d[u]+w[i],就可以接起来,把两个加在一起.. 如果d[v]>d[u]+w[i],那么c[v] ...
- TortoiseGit安装简单介绍和使用
首先,你必须有会装软件的技能和一个看得懂英语的眼睛.然后保证Git也装好了 他提供了中文版的安装包哦 安装过程尽量选择默认就行,先装上面那个啊,语言包最后装. 语言配置 因为以前装过,所以...路径是 ...
- Android 选择本地图片的demo
https://github.com/lipanquan/SelectLocalPhoto
- Go语言关键字之1--range
https://blog.csdn.net/iamlihongwei/article/details/78842857 https://studygolang.com/articles/1952 ht ...
- Deep Visualization:可视化并理解CNN(转)
转载地址:https://zhuanlan.zhihu.com/p/24833574 一.前言 CNN作为一个著名的深度学习领域的“黑盒”模型,已经在计算机视觉的诸多领域取得了极大的成功,但是,至今没 ...
- SElinux学习记录
1.SELinux:是一种基于域类型模型的强制访问控制安全系统,由NSA编写设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SE Linux补丁 查看Selinux: ps -Z #查看S ...
- my26_Slave failed to initialize relay log info structure from the repository
重启了一下从库,忘记先stop slave ,直接mysqladmin shutdown关闭实例,结果起不来了 mysql> start slave;ERROR 1872 (HY000): Sl ...
- docker 部署公司阿里云服务器 (一)
持续更新... 背景环境: 阿里云ecs服务器 centos7.4 公网地址:xx.xx.xx.xx 内网地址:172.16.77.4 阿里云RDS 阿里云 Redis 第 ...
- shell 命令下载软件 安装软件
下载命令:wget URL地址 wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-3.2.29-81.el6.centos.noa ...
- jemeter排至数据库时报:Access denied for user 'root'@'localhost' (using password:YES) 解决方案
相信这个问题大部分人都遇到过,至少我遇到过三次了,而且每次原因都不一样,前段时间同学也遇到这个问题,问我怎么解决,我把我的解决思路都说了一遍,发现还不行,最后居然是另外一个原因...哎,说多了都是泪, ...