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 ...
随机推荐
- C++实现委托机制(二)
1.引言: 上一篇文章已经介绍了如何构建一个无参数无返回值的函数指针的委托,这篇文章将对上一文章所述委托进行扩展,使得可以注册任意函数指针,不过再讲篇内容之前先要介绍一下实现这个功能所需要了解的C++ ...
- C++primer习题--第1章
本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter1-ans.html,转载请注明源地址. [习题 1.3] 编一个程序,在标准输出 ...
- QtGui.QColorDialog
he QtGui.QColorDialog provides a dialog widget for selecting colour values. #!/usr/bin/python # -*- ...
- linux sheel重复执行上条命令
Linux系统下Shell重复执行上条命令的 4 种方法: 1.使用上方向键,并回车执行. 2.按 !! 并回车执行. 3.输入 !-1 并回车执行. 4.按 Ctrl+P 并回车执行.
- a标签添加点击事件
a标签添加点击事件 CreateTime--2017年8月8日09:11:34 Author:Marydon 一.基础用法 方式一:(不推荐使用) <a href="javascr ...
- 系统学习NIO
概述 适用于有一定编程基础的朋友,想系统学习NIO这块知识的朋友.知识点大体分3块:1:>概念了解(各类IO) 2>NIO的核心(缓存区,通道等) 3>网络IO 详细 代码下载:ht ...
- 基于Ant Design UI框架的React项目
概述 这款基于React开发的UI框架,界面非常简洁美观,在这篇文章中我主要为大家介绍一下如何用Ant开始搭建React项目 详细 代码下载:http://www.demodashi.com/demo ...
- 【jquery操作cookie】JQuery中$.cookie()方法的使用(同名cookie会覆盖)
jquery.cookie.js插件: <script type="text/javascript" src="js/jquery-1.6.2.min.js&quo ...
- Entity Framework插入中文数据到MySQL乱码问题
1.MYSQL: 保证所有的的列都是UTF8格式. 2.在连接MySQL的连接字符串中,加入配置文件信息:Character Set=utf8
- Linux-软件包管理-yum在线管理-网络yum源
cd /etc/yum.repos.d/ 切换到etc目录下面的yum.repos.d这个目录中ls 查看当前linux系统的yum源文件信息,其中CentOS-Base.repo文件为默认的y ...