Given a linked list, determine if it has a cycle in it.

Have you met this question in a real interview?

Yes

Example

Given -21->10->4->5, tail connects to node index 1, return true

Challenge

Follow up:
Can you solve it without using extra space?

来源: http://www.lintcode.com/en/problem/linked-list-cycle/

分析


使用快慢指针

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*x;

    所以可以知道,LenA + x =  n*R;  LenA = n*R -x;  亦可以看成 LenA = m*R + y

4.求有环单链表的链表长

  上述2中求出了环的长度;3中求出了连接点的位置,就可以求出头结点到连接点的长度。两者相加就是链表的长度。

来源: http://www.cnblogs.com/xudong-bupt/p/3667729.html

null

转载于:https://www.cnblogs.com/zhxshseu/p/f8973e488b1e794e4312d1915ba90381.html

Linked List Cycle相关推荐

  1. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. 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 ...

  3. 141. Linked List Cycle

    欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...

  4. leetcode: Linked List Cycle II

    http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. leetcode - Linked List Cycle

    题目:Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solv ...

  9. 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 ...

  10. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

最新文章

  1. 创新课程管理系统数据库设计心得
  2. 图像水平梯度和竖直梯度代码_20行代码发一篇NeurIPS:梯度共享已经不安全了
  3. 阿里云物模型层功能分析
  4. C++11之异步调用
  5. html语言中空格用什么表示,HTML中的5种空格各表示的意义
  6. Hbase Java API详解
  7. 内存不足:杀死进程或牺牲孩子
  8. Introduction-To-Signal-Processingorfanidis-i2sp
  9. java常用的搜索引擎_我掏空了各大搜索引擎,给你整理了154道Java面试题!
  10. win32开发(绘制bitmap)
  11. linux link path walk,python之os.walk()与os.path.walk()
  12. C#中常见的数据结构简单介绍
  13. angular自定义管道
  14. MySQL之表的约束(主键、外键、唯一键、自增长、列描述、默认值、空属性)
  15. html 隐藏广告代码,Javascript实现关闭广告实现删除广告的效果
  16. centos7 RAID磁盘阵列卡驱动安装图文教程
  17. dns 监控系统 设计 dns安全威胁的可视化。
  18. 微软浏览器Edge不能上网的解决办法
  19. oracle12c cdb修改,ORACLE 12C 新特性CDB及PDB
  20. oracle 分区之 interval range 分区

热门文章

  1. Python安装Matplotlib,wordcloud,jieba第三方库
  2. CentOS7安装Oracle11g R2全过程
  3. it计算机哪些专业术语,IT之家学院:笔记本电脑专业术语科普
  4. eplan怎样创建和修改图框_EPLAN标题页及图框的设计
  5. Linux wc命令用于计算字数。
  6. Oracle存储过程及调用
  7. 为什么会用这个工具的产品经理,越来越值钱?
  8. 2020年中国在线少儿英语培训市场研究报告
  9. zabbix api java_zabbix的Java API(一)
  10. docker 仓库镜像 替换_Docker私有仓库 Registry中的镜像管理