算法:Linked List Cycle(环形链表)
说明
算法:Linked List Cycle
LeetCode地址:https://leetcode.com/problems/linked-list-cycle/
题目:
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?
解题思路1
环形链表只要判断是否经过相同的对象,用到去重容器Set来存储已经遍历的数据,遍历链表,判断是否存在于Set中。因为需要Set容器,空间复杂度为O(n), 时间复杂度为O(n).
代码实现1
import java.util.HashSet;
import java.util.Set;class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class LinkedListCycle {public boolean hasCycle(ListNode head) {Set<Integer> pastSet = new HashSet<>();while (head != null) {if (pastSet.contains(head.val)) {return true;} else {pastSet.add(head.val);head = head.next;}}return false;}
}
代码执行效率1
Runtime: 6 ms, faster than 11.95% of Java online submissions for Linked List Cycle.
Memory Usage: 35.4 MB, less than 99.35% of Java online submissions for Linked List Cycle.
解题思路2
题目要求是内存复杂度在O(1), 也就是不能用Set容器。需要用指针的方法处理:
用两个步长分别为slowStep = 1,和fastStep = 2的指针,如果能遍历到下个结点是null,说明不是循环列表. 循环列表里面,两个步长的相差为1,肯定会相遇。
证明:异常情况:两个步长不相遇的情况,只有一种可能,步长1在步长2的节点前面相差1. 循环区间的长度为K,那么K有两种情况,要么是奇数,要么是偶数。
先假设遍历的位置,slowIndex = 1, fastIndex = 0;(实际上slowIndex = 2, fastIndex = 1也是相同的证明方法)
K == 奇数:fastStep到终点,slowIndex = 1 + ½ * (k - 1) , fastIndex = k - 2; 再走步数 ½ * (k - 1) , slowIndex = k, 也就是 slowIndex = 0, fastIndex = k - 1; 下一步 slowIndex == fastIndex == 1.
K == 偶数:fastStep到终点,slowIndex = 1 + ½ * k , fastIndex = k - 1; 再走步数½ * k,slowIndex = 1 + k, 相当于 slowIndex = 1 , fastIndex = k, 相当于 fastIndex = 0; 下一步 slowIndex == fastIndex == 1.
空间复杂度为O(1); 时间复杂度为 O(n) , 因为循环区间次数假设为k,实为O(n+k).
代码实现2
public class LinkedListCycle {public boolean hasCycleWithTwoPoint(ListNode head) {if (head == null || head.next == null) {return false;}ListNode slowNode = head;ListNode fastNode = head.next;while (slowNode != fastNode) {if (slowNode == null || fastNode == null || fastNode.next == null) {return false;}slowNode = slowNode.next;fastNode = fastNode.next.next;}return true;}
}
代码执行效率2
Runtime: 0 ms, faster than 100.00% of Java online submissions for Linked List Cycle.
Memory Usage: 39.6 MB, less than 5.15% of Java online submissions for Linked List Cycle.
总结
考查循环链表,和空间复杂度的优化。
代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/popular/LinkedListCycle.java
算法:Linked List Cycle(环形链表)相关推荐
- 141. Linked List Cycle 环形链表
给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引 ...
- java环形链表_数据结构和算法(四)Java实现环形链表
1. 数据结构和算法(四)Java实现环形链表 1.1 约瑟夫问题 约瑟夫问题:公元66年,约瑟夫不情愿地参与领导了犹太同胞反抗罗马统治的起义,后来起义失败,他和一些宁死不降的起义者被困于一个山洞之中 ...
- 链表(Linked List)之环形链表
原文地址:传送门 单向环形链表应用场景 Josephu(约瑟夫.约瑟夫环) 问题 Josephu 问题为:设编号为1,2,- n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始 ...
- [LeetCode] 141. Linked List Cycle 单链表判圆算法
TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...
- 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 ex ...
- 【算法】Hash实现环形链表【LeetCode】
一. 题目:给定一个链表,判断链表中是否有环. 条件:如果链表中存在环,则返回 true . 否则,返回 false . 二.题解 链表结点 class ListNode {* int val;* L ...
- 【LeetCode】详解环形链表141. Linked List Cycle Given a linked list, determine if it has a cycle in it. To
文章目录 前言 正文 原题: 思路1: 思路2 思路3 总结 前言 今天这道题目的第一种解法很奇葩,用计时器竟然可以AC,并且可以自己调整时间多少,跟我一起来看看吧. 正文 原题: 链接:环形链表 G ...
- 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 lin ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 数据结构与算法(二)单链表(Singly linked list)
数据结构与算法(二)单链表(Singly linked list) 链表(Linked list) Python完整功能实现 LeetCode思想实践: 链这个东西大家一定都不陌生, 脖子上有项链, ...
最新文章
- Android中View绘制流程以及invalidate()等相关方法分析
- fork()子进程与父进程之间的文件描述符问题
- SQL:RAND()返回随机数
- 各种ARM仿真器接口图
- Java高并发编程:Callable、Future和FutureTask
- HJ12 字符串反转
- 【学术研讨课】雷诺实验2018-06-13
- Oracle linux R5-U7中YUM 源配置
- Input placeholder属性样式修改(颜色,大小,位置)
- [置顶]android ListView包含Checkbox滑动时状态改变
- linux 脚本 数字运算符,Shell 基本运算符
- 出现类似于 ”IDD_DLG_WAIT 未声明的标识符“ 这样的错误怎么办
- 实验一 Linux开发环境的配置 20145213祁玮 20145222黄亚奇
- Sublime Text 2 代码编辑器使用技巧
- Dmp文件导入(Imp命令)
- php上证指数抽奖代码,上证指数etf基金代码,场内etf基金一览表
- 我看QQ与360之争
- 洛谷 P1564 膜拜 题解
- Intellij IDEA 初学入门图文教程(六) —— IDEA 在 Performing VCS Refresh 卡死
- 怎么设置织梦栏目html结尾,dedecms网站栏目地址url优化成.html结尾的而不是文件夹形式结尾的。请大家来帮忙。...