LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
判断链表是否有环:使用快慢指针去判断,如果有环,fast快指针先进入环,slow慢指针后进入,经过一定步的操作,快慢指针在环上相遇。
方法一:(C++)
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast=head,*slow=head;
while(fast&&fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
return true;
}
return false;
}
};
方法二:(Java)
public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> s=new HashSet();
while(head!=null){
if(s.contains(head))
return true;
else
s.add(head);
head=head.next;
}
return false;
}
}
Java集合中含有contains()方法,判断其是否已经含有此节点,如果没有,则使用add()添加进去
LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java的更多相关文章
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ...
- 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 ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 141 Linked List Cycle(判断链表是否有环Medium)
题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- [Leetcode] Linked list cycle 判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 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 ...
随机推荐
- creator Box2d的相关物理问题
项目的屏幕配置为 1152*640,没有对这个数值调整来测试质量的除数1024会不会变化 m = h * w / 1024 (m 质量,h 物体高度,w 物体宽度) g = 0, -320 ( ( ...
- Linux第十节课学习笔记
部署LVM三步: 1.pv:使设备支持LVM: 2.vg:对支持LVM的设备进行整合: 3.lv:将整合的空间进行切割. 每个基本单元PE的大小为4M,分配空间必须是4M的整数倍.可以容量或基本单元个 ...
- c# Linq&Lambda
0.写这个文章主要记录下常用Lambda的用法,能力有限,文中有问题的地方希望各位大神指出来谢谢!因为平时写代码的时候没有特地去用lambda,全是用一些循环,少量会用到lambda,虽然也能实现要的 ...
- Centos7快速部署saltstack
saltstack是一个和ansible差不多的自动化运维工具,可以用来批量管理大量主机 OS:centos7.3 server:172.16.13.159 client: 172.16.13.156 ...
- python 常用库
Numpy, Pandas 数据处理 Scipy 科学计算 Matplotlib 可视化 Scikit Learn 机器学习 Keras 深度学习模型 jieba 分词 Gensim 主题(包含 ...
- python之计算机硬件基本认知_数据单位_进制间转换_数的原码反码补码
一:计算机硬件基本认知 cpu: 中央处理器. 相当于人的大脑.运算中心,控制中心. 内存: 临时存储数据. 优点:读取速度快,缺点:容量小,造价高,断电即消失. 硬盘: 长期存储数据. ...
- mysql入门学习笔记
MySQL的登陆和退出 mysql -u 用户名 -p 密码 #登陆 quit #退出(exit or \q) 具体参数: 参数 描述 -D,--database=name 打开指定数据库 -deli ...
- Java ConcurrentHashMap存入引用对象时也是线程安全的
本人小白,看到资料说ConcurrentHashMap是线程安全的,get过程不需要加锁,put是线程安全的,推荐高并发时使用.但是本人不清楚是否该map中存入的引用类型对象,对象属性变化也是否线程安 ...
- Linux基础入门-环境变量与文件查找
一.环境变量: Shell中的变量也有不同的数据类型,不用专门指定类型名,可以参与运算,有作用域限制. declare tmp 创建一个变量 可以不用专门声明,可以即用即创建 tmp=shiyanlo ...
- mysql合并表
有如下两张表 a +------+------+---------+ | uid | name | addtime | +------+------+---------+ | | | +------+ ...