LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)
Time:2019/4/7
Title: Linked List Cycle
Difficulty: Easy
Author:小鹿
题目:Linked List Cycle I
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.
复制代码
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
Slove:
▉ 算法思路:
两种解题思路:
1)哈希表法:遍历链表,没遍历一个节点就要在哈希表中判断是否存在该结点,如果存在,则为环;否则,将该结点插入到哈希表中继续遍历。
2)用两个快慢指针,快指针走两步,慢指针走一步,如果快指针与慢指针重合了,则检测的当前链表为环;如果当前指针或下一指针为 null ,则链表不为环。
▉ 方法一:哈希表
/*** Definition for singly-linked list.* function ListNode(val) {* this.val = val;* this.next = null;* }*//*** @param {ListNode} head* @return {boolean}*/var hasCycle = function(head) {let fast = head;let map = new Map();while(fast !== null){if(map.has(fast)){return true;}else{map.set(fast);fast = fast.next;}}return false;};复制代码
▉ 方法二:快慢指针
var hasCycle = function(head) {if(head == null || head.next == null){return false;}let fast = head.next;let slow = head;while(slow != fast){if(fast == null || fast.next == null){return false;}slow = slow.next;fast = fast.next.next;}return true;};
复制代码
▉ 方法二:快慢指针
这部分代码是我自己写的,和上边的快慢指针思路相同,运行结果相同,但是当运行在 leetcode 时,就会提示超出时间限制,仔细对比代码,我们可以发现,在逻辑顺序上还是存在差别的,之所以超出时间限制,是因为代码的运行耗时长。
//超出时间限制
var hasCycle = function(head) {if(head == null || head.next == null){return false;}let fast = head.next;let slow = head;while(fast !== null && fast.next !== null){if(slow === fast) return true;slow = head.next;fast = fast.next.next;}return false;
};
复制代码
欢迎一起加入到 LeetCode 开源 Github 仓库,可以向 me 提交您其他语言的代码。在仓库上坚持和小伙伴们一起打卡,共同完善我们的开源小仓库! Github:https://github.com/luxiangqiang/JS-LeetCode
欢迎关注我个人公众号:「一个不甘平凡的码农」,记录了自己一路自学编程的故事。
LeetCode 之 JavaScript 解答第141题 —— 环形链表 I(Linked List Cycle I)相关推荐
- 《LeetCode力扣练习》第141题 环形链表 Java
<LeetCode力扣练习>第141题 环形链表 Java 一.资源 题目: 给你一个链表的头节点 head ,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)
Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目:Merge K Sorted Lists ...
- LeetCode 之 JavaScript 解答第98题 —— 验证二叉搜索树(Validate Binary Search Tree)
Time:2019/4/24 Title: Vaildata Binary Search Tree Difficulty: Medium Author: 小鹿 题目:Vaildata Binary S ...
- LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)
Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...
- <力扣(LeetCode)>141、环形链表(链表基础解法)java
141. 环形链表 给你一个链表的头节点 head ,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,评测系统内部使用整 ...
- leetcode算法题--环形链表 II★
原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...
- C#LeetCode刷题之#141-环形链表(Linked List Cycle)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...
- Leetcode每日一题:141.linked-list-cycle(环形链表)
思路1:这道题如果就这么遍历下去,那么出现一个环就是死循环了,所以想到一个骚办法,也算是投机取巧吧,把遇到的每个数字都置INT32_MAX,在置之前先判断这个值是不是INT32_MAX,如果是则说明这 ...
最新文章
- 怎么理解ASM中的Failgroup
- K近邻算法:以同类相吸解决分类问题!
- 在你的Fckeditor安装Google map plugin
- Spring 原理初探——IoC、AOP
- win7下查找端口,使用netstat命令注意事项
- 删除所有的distribution chain再保存的后台执行逻辑
- 肺部胸片图像掩膜和伪彩色处理matlab
- 在Windows 7 Ultimate 64位上安装Java
- 2018-2019-1 20165319 《信息安全系统设计基础》第八周学习总结
- dedecms 会员发布的信息后台未审核,前台显示审核修改
- PHP报错:Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars
- 【配送路径规划】基于matlab遗传算法求解单配送中心多客户多车辆最短路径规划问题【含Matlab源码 1602期】
- 效果器的使用技巧-与调音台的无缝连接
- 国开大学计算机实操,国开大学计算机实操答案一 .pdf
- 清华大学2019年“全国优秀中学生信息学冬季体验营”报名通知
- Go面向对象---音乐库案例
- 感知机——鸢尾花 包含代码
- 科学计算机技术标准差,自动化技术_计算机技术_
- 二分图判断以及二分图最大匹配
- mysql 怎么反选_全选与反选
热门文章
- 吉林大学计算机科学与技术学院王欣,应用改进迭代最近点方法的点云数据配准...
- native react 折线图_react native中使用echarts
- Source Insight 使用教程(1)——新建项目
- 给Win7光盘添加PE3.0
- 单臂路由配置实验同一交换机上vlan间ping不通_【干货】什么是单臂路由?如何配置?...
- PHP域名查墙代码,怎么查看域名是否被墙检测(教你一招域名被墙解决办法)
- php100视频教程2012,PHP100视频教程2012新版
- 没有运行 spring_Spring事务的传播行为案例分析
- mysql查询递增列_mysql在查询结果列表前添加一列递增的序号列(最简)
- 思科怎么隐藏端口_这些著名商标下的隐藏设计,你能发现吗?