【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点
【试题描述】定义一个函数,输入一个链表,判断链表是否存在环路,并找出回路起点
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笔试面试题整理】判断链表是否存在环路,并找出回路起点的更多相关文章
- 【IT笔试面试题整理】链表
如何准备 Linked list questions are extremely common These can range from simple (delete a node ina linke ...
- 【IT笔试面试题整理】判断一个二叉树是否是平衡的?
[试题描述]定义一个函数,输入一个链表,判断链表是否存在环路 平衡二叉树,又称AVL树.它或者是一棵空树,或者是具有下列性质的二叉树:它的左子树和右子树都是平衡二叉树,且左子树和右子树的高度之差之差的 ...
- Java笔试面试题整理第六波(修正版)
转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第八波
转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第四波
转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第三波
转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第二波
转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第五波
转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第一波
转载至:http://blog.csdn.net/shakespeare001/article/details/51151650 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
随机推荐
- java基础-day33
第10天 Transaction事务 今日内容介绍 u 事务管理 u 转账案例 u 事务总结 第1章 事务管理 1.1 事务概述 l 事务指的是逻辑上的一组操作,组成这组操作的各个单元要么全都 ...
- Northwind测试学习用数据库
下载地址: https://northwinddatabase.codeplex.com/
- 冲刺博客NO.3
今天做了什么:参考网上的一些登录界面,发现了Android studio自带loginActivity.做了基础登录界面 不停地上网搜,各种不会. 在短信验证功能上通过在Mob.com的集成文档和官 ...
- unigui的ServerModule常用属性设置
unigui的ServerModule常用属性设置 1)压缩设置 compression是压缩数据用的.默认启用压缩,且压缩级别是最大的. 2)UNIGUI运行时库设置 UNIGUI需要4个运行时库, ...
- FastReport报表设计(仔细看)
FastReport报表设计 2011-06-16 16:56:19| 分类: 系统开发|举报|字号 订阅 下载LOFTER我的照片书 | 目录 5.1 前言 5.2 基本概念及操 ...
- powerviot open error in sharepoint 2013
Testing Service c2WTS +- Service c2WTS found +- Service c2WTS is running +- Path of service: C:\Prog ...
- 201621123018《java程序设计》第12周作业总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2.1 简述如何 ...
- Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)
C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...
- underscore.js源码研究(1)
概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...
- flask_ Mongodb 的语法-排序
MOngoDB的排序是挺有用的 ,跟MySQL有明显的区别 .. 它的原生语法的第一个参数为条件限定,第二个参数为排序字段 db.news.find({},{'_id':1}) #1是升序 ...