【试题描述】定义一个函数,输入一个链表,判断链表是否存在环路,并找出回路起点

Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an
earlier node, so as to make a loop in the linked list
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C

If we move two pointers, one with speed 1 and another with speed 2, they will end up meet-ing if the linked list has a loop Why? Think about two cars driving on a track—the faster car
will always pass the slower one!
The tricky part here is finding the start of the loop Imagine, as an analogy, two people rac-ing around a track, one running twice as fast as the other If they start off at the same place, when will they next meet? They will next meet at the start of the next lap

Now, let’s suppose Fast Runner had a head start of k meters on an n step lap When will they next meet? They will meet k meters before the start of the next lap (Why? Fast Runner would have made k + 2(n - k) steps, including its head start, and Slow Runner would have made n - k steps Both will be k steps before the start of the loop )
Now, going back to the problem, when Fast Runner (n2) and Slow Runner (n1) are moving around our circular linked list, n2 will have a head start on the loop when n1 enters Specifi-cally, it will have a head start of k, where k is the number of nodes before the loop Since n2 has a head start of k nodes, n1 and n2 will meet k nodes before the start of the loop

So, we now know the following:
1   Head is k nodes from LoopStart (by definition)
2   MeetingPoint for n1 and n2 is k nodes from LoopStart (as shown above)
Thus, if we move n1 back to Head and keep n2 at MeetingPoint, and move them both at the
same pace, they will meet at LoopStart

分析:为何能够找到环的起始位置?

假设环的长度是 m, 进入环前经历的node的个数是 k , 那么,假设经过了时间 t,那么速度为2 的指针距离起始点的位置是:  k + (2t - k) % m = k + (2t - k) - xm . 同理,速度为1的指针距离起始点的位置是 k + (t - k) % m = k + (t - k) - ym。

如果 k + (2t - k) - xm =  k  + (t - k) - ym ,可以得到 t = m (x - y)。 那么当t 最小为m的时候,也就是说,两个指针相聚在 距离 起始点 m - k的环内。换句话说,如果把一个指针移到链表的头部,然后两个指针都以 1 的速度前进,那么它们经过 k 时间后,就可以在环的起始点相遇。

【参考代码】

 1     public static boolean judgeList(LinkList myList)
 2     {
 3         Link fast, slow;
 4         fast = slow = myList.first;
 5         while (true)
 6         {
 7             if (fast == null || fast.next == null)
 8                 return false;
 9             else if (fast == slow || fast.next == slow)
10                 return true;
11             else
12             {
13                 slow = slow.next;
14                 fast = fast.next.next;
15             }
16         }
17     }
 1     public static Link FindBeginning(LinkList myList)
 2     {
 3         Link fast, slow;
 4         fast = slow = myList.first;
 5         while(fast.next !=null)
 6         {
 7             slow = slow.next;
 8             fast = fast.next.next;
 9             if(slow == fast)
10                 break;
11         }
12
13         if(fast.next == null)
14             return null;
15         /* Move slow to Head. Keep fast at Meeting Point. Each are k steps
16         /* from the Loop Start. If they move at the same pace, they must
17          * meet at Loop Start. */
18         slow = myList.first;
19
20         while(slow!=fast)
21         {
22             slow = slow.next;
23             fast = fast.next;
24         }
25
26         return fast;
27     }

【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点相关推荐

  1. 【IT笔试面试题整理】链表

    如何准备 Linked list questions are extremely common These can range from simple (delete a node ina linke ...

  2. 如何判断链表中存在环路

    如果你曾经想过要参加面试,像我一样,你一定看过这个问题:如何判断链表中存在环路.(我不太清楚这个问题的应用在哪里,烦请各位读者能够提示一下.) 先简单说一下我之前看到的方法. 方法一:蛮力法. 方法二 ...

  3. 如何判断链表是否有环,确定环的起点

    如何判断链表是否有环,确定环的起点 目录 判断链表是否有环 如何找到环的入口 判断链表是否有环 使用快慢指针确定链表是否有环 快指针(fast) 慢指针(slow) 思路: 让快指针和慢指针从链表的起 ...

  4. 网易历届笔试面试题整理大全

    整理了一下网易往届笔试面试题,希望对大家有帮助: 超级有用的面试题:Java常见面试题    常见算法面试题   数据库常见面试题  操作系统常见面试题   C/C++常见面试题  大数据常见面试   ...

  5. 面试题:判断链表是否存在环

    题目:判断链表是否存在环 思路:定义快慢指针,如果两个指针相遇则一定存在环. 1 public bool IsCircled(Node First) 2 { 3 if (First == null | ...

  6. 【IT笔试面试题整理】位操作

    如何准备: Bit manipulation can be a scary thing to many candidates, but it doesn't need to be! If you're ...

  7. 【IT笔试面试题整理】数组中出现次数超过一半的数字

    [试题描述]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. [试题分析]时间复杂度O(n),空间复杂度O(1) 思路1: 创建一个hash_map,key为数组中的数,value为此数 ...

  8. 链表:判断链表是否有环以及找入口

    141环形链表 题意: 给定一个链表,判断链表中是否有环.如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连 ...

  9. 笔试算法题(28):删除乱序链表中的重复项 找出已经排好序的两个数组中的相同项...

    出题:给定一个乱序链表,节点值为ASCII字符,但是其中有重复项,要求去除重复项并保证不改变剩余项的原有顺序: 分析:创建一个256(2^8)大小的bool数组,初始化为false,顺序读取链表,将字 ...

最新文章

  1. ZooKeeper 源码和实践揭秘
  2. linux进程调度浅析
  3. 研究人员使用脑机接口从神经信号中重建单词
  4. BeeHive模块注册
  5. Visual Studio 2017 图文安装流程
  6. 0408互联网新闻 | 区块链医疗解决方案落地应用,阿里云、支付宝共同研发;安卓全球开发者峰会将于10月23-24日在加州召开...
  7. Java web 面试题
  8. python 量化交易_Quantsrat让R语言像Python一样进行策略回测和量化交易
  9. [Godot] v4.0.alpha1 GDScript 实现第三人称控制器
  10. python cx_oracle配置_用Python设置cx_Oracle环境变量
  11. 场论 梯度 旋度 散度
  12. Android 两个App间进行IPC通信
  13. JavaScript:加载请求本地资源工具StaticResourceUtil.js
  14. 【BZOJ 3442】 3442: 学习小组 (最大费用流)
  15. DAHON 美国大行
  16. Atitit 提升记忆效率 有损压缩原理总结 目录 1. 常见方法 1 1.1. 抽象化提升一层 概念化 1 1.2. 骨架 ,目录化 大纲化 归纳整理 1 1.3. 提取关键词 ,摘要 ,
  17. 【Python精彩案例】随拍文档转PDF扫描版
  18. 计算机课搞事情检讨,考试作弊被抓写的检讨书(精选10篇)
  19. 用MATLAB实现高斯投影正反算且画出高斯投影图形
  20. Win11安装OBS Studio的详细步骤图文教程

热门文章

  1. UI组件库从1到N开发心得-组件篇
  2. 阿里员工都在用的知识管理工具,究竟有何特别?
  3. ASP.NET MVC下使用AngularJs语言(三):ng-options
  4. JBoss5 启动报错java.lang.IllegalArgumentException: ...
  5. 【RIA Services】系统分析
  6. QQ病毒越来越人性化了
  7. 云计算与SaaS的关系 :新计算时代将来临
  8. [听尉迟方侃侃]平台
  9. ups计算软件_ups不间断电源系统分类及作用
  10. request.getParameterMap的学习