当前位置:网站首页>笔试面试题目:判断单链表是否有环
笔试面试题目:判断单链表是否有环
2020-11-08 10:30:00 【osc_ezb5qfcz】
原文发表于:

之前在U公司的笔试中,碰到这样一个问题:
判断单链表是否有环。
首先来看这样一个常识:现实中的环路与单链表的环路,有什么不同呢?
显然:现实中的环路,可以有两个方向,要么循环,要么逃出。然而,在单链表中,指针next只可能有一个指向,所以环路链表必定永远循环,没有出口。如下图所示:

回到问题本身,怎么判断单链表是否有环呢?
算法1:标记法
最容易想到的肯定是标记法。遍历链表时,对访问过的结点做记录。如果是环状单链表,则必然有结点被重复访问。这种思路是非常自然的。
做标记时,可考虑用hash map或者hash set, 需要耗费一些空间。由于思路比较明确,所以就没有必要详细介绍程序了。
算法2:暴力算法
暴力,也是解决问题的一种思路,尽管不一定是最好的方式。
可以这么考虑:一路走到黑,如果到了终点,则没有环,如果没有到达终点,则说明在环中不停绕圈。
我刚才在leetcode上做了一下这个题目,用的是暴力法,能通过所有测试用例,代码如下:
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
count := 0
max := 12000
for ; head != nil && count < max; {
count++
head = head.Next
}
if count == max {
return true
}
return false
}
一看就知道,这种暴力算法是有局限性的:比如,如果链表足够长,也会导致count和max相等,可能会误判的。
算法3:快慢指针
摩托车在后,自行车在前,摩托车追赶自行车,如果是一个没有出路的环形路,那么摩托车必然会追上自行车。
所以,我们可以采用类似思路,让快指针在后,慢指针在前,可以判断单链表是否带环。这种方法的好处是,空间复杂度是O(1),且不会出现误判。
既然知道了思路,写程序就是很简单的事情了,故不再赘述。
周末愉快,下周再聊,不见不散。
版权声明
本文为[osc_ezb5qfcz]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4309139/blog/4707995
边栏推荐
- Japan PSE certification
- IOS learning note 2 [problems and solutions encountered during the installation and use of cocopods] [update 20160725]
- It's 20% faster than python. Are you excited?
- Rust:命令行参数与环境变量操作
- Function periodic table filter value selectedvalue
- Japan PSE certification
- “智能5G”引领世界,数位智能网优+5G能带来什么?
- Astra: the future of Apache Cassandra is cloud native
- Personal current technology stack
- SQL Server 2008R2 18456 error resolution
猜你喜欢

Insight -- the application of sanet in arbitrary style transfer

5g + Ar out of the circle, China Mobile Migu becomes the whole process strategic partner of the 33rd China Film Golden Rooster Award

2 days, using 4 hours after work to develop a test tool

FORTRAN 77 reads some data from the file and uses the heron iteration formula to solve the problem

个人目前技术栈

NOIP 2012 提高组 复赛 第一天 第二题 国王游戏 game 数学推导 AC代码(高精度 低精度 乘 除 比较)+60代码(long long)+20分代码(全排列+深搜dfs)

Flink的sink实战之一:初探

Search and replace of sed

Oops, the system is under attack again

Rust: command line parameter and environment variable operation
随机推荐
Function periodic table filter value selectedvalue
Solve Safari browser download file name garbled problem
Template linked list learning
More than 50 object detection datasets from different industries
Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
ArrayList源码分析
next.js实现服务端缓存
Game mathematical derivation AC code (high precision and low precision multiplication and division comparison) + 60 code (long long) + 20 point code (Full Permutation + deep search DFS)
IOS learning note 2 [problems and solutions encountered during the installation and use of cocopods] [update 20160725]
Which is more worth starting with the difference between vivos7e and vivos7
PCR and PTS calculation and inverse operation in TS stream
vivoy73s和荣耀30青春版的区别
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter
C++在C的基础上改进了哪些细节
解决Safari浏览器下载文件文件名称乱码的问题
Installing MacOS 11 Big Sur in virtual machine
Mate 40系列发布 搭载华为运动健康服务带来健康数字生活
ulab 1.0.0发布
Game optimization performance (11) - Zhihu