141. Linked List Cycle(判断链表是否有环)
2024-08-27 02:28:43
141. 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?
利用快慢指针,如果相遇则证明有环
注意边界条件: 如果只有一个node.
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null) return false;
ListNode slower =head,faster = head;
while(faster!=null && faster.next!=null){
if(faster==slower) return true;
faster = faster.next.next;
slower = slower.next;
}
return false;
}
}
最新文章
- Cannot connect to the Docker daemon. Is the docker daemon running on this host?
- Python 爬虫 字符集乱码问题
- Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别
- wc
- Windows 提高效率的常用快捷键
- 《C与指针》第四章练习
- [USACO2004][poj2373]Dividing the Path(DP+单调队列)
- AngularJs中的服务
- Drupal8开发教程:模块开发——创建新页面
- [Codeforces] 347B - Fixed Points
- C#线程总结
- java 小结1(static ,final,泛型)
- python -i filename
- c#中传递参数前加out
- 另一种数据库连接字符串的编写方式(Sqlbuilder)
- db_link
- 如何应对苹果app 的ipv6 时代?腾讯专家教您进行环境改造
- 自学Python5.1-模块简介
- 强化学习(十一) Prioritized Replay DQN
- css文字超出一行用点表示
热门文章
- com.thoughtworks.xstream.converters.ConversionException
- Linux alias 命令
- IDEA Intellij 打开springboot项目 配置文件无法出现输入提示
- JavaScript的记忆函数真的可以提升性能吗?
- poj_3283 trie树
- Mybatis——SQL语句构建器类
- WEB安全第四篇--与数据库的亲密接触:SQL注入攻击
- Eclipse 真机调试检测不到手机解决方案
- jhipser微服务架构介绍
- PHP unlink()函数,删除文件