LeetCode -- 推断链表中是否有环
思路:
使用两个节点。slow和fast,分别行进1步和2步。假设有相交的情况,slow和fast必定相遇;假设没有相交的情况,那么slow或fast必定有一个为null
相遇时有两种可能:
1. 仅仅是节点相交的情况,即:slow == fast可是 slow.next != fast.next
2. 链表中存在环,即slow == fast 并且 slow.next == next
实现代码:
public bool HasCycle(ListNode head) {
// - for null node , false
if(head == null || head.next == null){
return false;
}
if(head.val != head.next.val && head.next.next == null){
return false;
}
var slow = head;
var fast = head;
while(true) {
slow = slow.next;
if(fast.next != null){
fast = fast.next.next;
}
else{
return false;
}
if(slow == null || slow.next == null || fast == null || fast.next == null) {
return false;
}
if(slow.val == fast.val && slow.next.val == fast.next.val){
return true;
}
}
return false;
}
LeetCode -- 推断链表中是否有环的更多相关文章
- Q:判断链表中是否存在环的相关问题
问题:如何判断一个单向链表中是否存在环? 例如: 链表中存在环(B-->D): <-- <--^ | | v | A-->B-->C-->D 链表中不存在环: A- ...
- 查找链表中是否有环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 -- 删除链表中值为k的元素
本题目比較直接,一次遍历遇到匹配的元素直接删除(通过n.next = n.next.next)就能够了,仅仅是须要考虑到:1.首节点的情况2.末节点的情况 下面为实现: public ListNode ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- 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 ext ...
- [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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
随机推荐
- 解决eclipse中运行web项目时弹出的"Port 8080 required by Tomcat 9.0 Server at localhost is already in use...
1.tomcat默认端口是8080,可以修改通过tomcat的端口 修改tomcat\conf\server.xml 结果运行程序,还是报"Port 8080 required by ...
- [React] Use the useReducer Hook and Dispatch Actions to Update State (useReducer, useMemo, useEffect)
As an alternate to useState, you could also use the useReducer hook that provides state and a dispat ...
- apple iMac一体机 装双系统 实战! (Apple +Win 7 64bit)Good
原帖:http://group.zol.com.cn/1/641_485.html 需要准备工具:一个8GU盘X1 (或Win7 安装版系统盘) Windows 7 pro 64bit 位 ISO 文 ...
- 自己定义UIView以实现自绘
有时候我们须要自绘uiview以实现自己的需求,比方依据坐标点绘制出连续的曲线(股票走势图),就须要自绘uiview了. 原理:继承uiview类(customView),并实现custom view ...
- [转]K近邻算法
什么是K近邻算法 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1时,算法便成了最近邻算法,即 ...
- 前端project与性能优化(长文)
原文链接:http://fex.baidu.com/blog/2014/03/fis-optimize/ 每一个參与过开发企业级 web 应用的前端project师也许都曾思考过前端性能优化方面的问题 ...
- oracle 三表关联查询
oracle 三表关联查询 CreationTime--2018年7月4日17点52分 Author:Marydon 左连接实现三表关联 表A--------------------------- ...
- 【Android】在build/envsetup.sh中添加自己的命令(函数)
由于测试的需要,经常需要手动刷入boot.img和system.img,把它写到envsetup.sh就可以每次使用一行命令来代替了: function flashtestimage(){ if [[ ...
- 怎样获取oracle dbid
1.查询v$database获得 因为DBID在控制文件和数据文件里都存在记录,所以假设可以mount数据库就行查询v$database视图获得. SQL> alter database mo ...
- Android自动化框架
Android自动化框架 已有 2085 次阅读2014-8-26 12:19 | Android 几种常见的Android自动化测试框架及其应用 随着Android应用得越来越广,越来越多的公司推出 ...