Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Given -21->10->4->5, tail connects to node index 1, return true
Follow up:
Can you solve it without using extra space?
分析
使用快慢指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @return: True if it has a cycle, or false
*/
public boolean hasCycle(ListNode head) {
// write your code here
ListNode slow = head, fast = head;
while (fast != null && fast.next != null && fast.next.next != null ){
slow = slow.next;
fast = fast.next.next;
if (slow == fast){
return true ;
}
}
return false ;
}
}
|
1.判断单链表是否有环
使用两个slow, fast指针从头开始扫描链表。指针slow 每次走1步,指针fast每次走2步。如果存在环,则指针slow、fast会相遇;如果不存在环,指针fast遇到NULL退出。
就是所谓的追击相遇问题: 相遇时,slow 并没有走完一圈,fast领先slow一圈,第一次相遇在Pos处
2.求有环单链表的环长
在环上相遇后,记录第一次相遇点为Pos,之后指针slow继续每次走1步,fast每次走2步。在下次相遇的时候fast比slow正好又多走了一圈,也就是多走的距离等于环长。
设从第一次相遇到第二次相遇,设slow走了len步,则fast走了2*len步,相遇时多走了一圈:
环长=2*len-len。
3.求有环单链表的环连接点位置
第一次碰撞点Pos到连接点Join的距离 = 头指针到连接点Join的距离,因此,分别从第一次碰撞点Pos、头指针head开始走,相遇的那个点就是连接点。
在环上相遇后,记录第一次相遇点为Pos,连接点为Join,假设头结点到连接点的长度为LenA,连接点到第一次相遇点的长度为x,环长为R。
第一次相遇时,slow走的长度 S = LenA + x;
第一次相遇时,fast走的长度 2S = LenA + n*R + x;
所以可以知道,LenA + x = n*R; LenA = n*R -x; 亦可以看成 LenA = m*R + y
4.求有环单链表的链表长
上述2中求出了环的长度;3中求出了连接点的位置,就可以求出头结点到连接点的长度。两者相加就是链表的长度。
转载于:https://www.cnblogs.com/zhxshseu/p/f8973e488b1e794e4312d1915ba90381.html
Linked List Cycle相关推荐
- 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--单向链表成环的起点--C++,Python解法
题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...
- 141. Linked List Cycle
欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- leetcode day5 -- Reorder List Linked List Cycle II
1. Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...
- 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 ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...
- leetcode - Linked List Cycle
题目:Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solv ...
- Leetcode 142 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
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
最新文章
- 创新课程管理系统数据库设计心得
- 图像水平梯度和竖直梯度代码_20行代码发一篇NeurIPS:梯度共享已经不安全了
- 阿里云物模型层功能分析
- C++11之异步调用
- html语言中空格用什么表示,HTML中的5种空格各表示的意义
- Hbase Java API详解
- 内存不足:杀死进程或牺牲孩子
- Introduction-To-Signal-Processingorfanidis-i2sp
- java常用的搜索引擎_我掏空了各大搜索引擎,给你整理了154道Java面试题!
- win32开发(绘制bitmap)
- linux link path walk,python之os.walk()与os.path.walk()
- C#中常见的数据结构简单介绍
- angular自定义管道
- MySQL之表的约束(主键、外键、唯一键、自增长、列描述、默认值、空属性)
- html 隐藏广告代码,Javascript实现关闭广告实现删除广告的效果
- centos7 RAID磁盘阵列卡驱动安装图文教程
- dns 监控系统 设计 dns安全威胁的可视化。
- 微软浏览器Edge不能上网的解决办法
- oracle12c cdb修改,ORACLE 12C 新特性CDB及PDB
- oracle 分区之 interval range 分区
热门文章
- Python安装Matplotlib,wordcloud,jieba第三方库
- CentOS7安装Oracle11g R2全过程
- it计算机哪些专业术语,IT之家学院:笔记本电脑专业术语科普
- eplan怎样创建和修改图框_EPLAN标题页及图框的设计
- Linux wc命令用于计算字数。
- Oracle存储过程及调用
- 为什么会用这个工具的产品经理,越来越值钱?
- 2020年中国在线少儿英语培训市场研究报告
- zabbix api java_zabbix的Java API(一)
- docker 仓库镜像 替换_Docker私有仓库 Registry中的镜像管理