cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
快指针和慢指针一起在头指针开始移动,快指针每次移动两步,慢指针每次移动一步,直到相遇, 相遇后,把慢指针指向头指针,两个指针一起往后移动一步。直到相遇,相遇的地方就是循环的开始(如果相遇,说明是有循环的节点,这时快指针走的路程是慢指针的两倍,快:开始的k距离,和一圈(或者n圈)循环,和慢指针走过的循环圈的路部分长度这三部分相加。。。
看图:

慢指针走的距离是 开始没进循环的k距离慢指针走过的循环圈的路部分长度 这两部分相加),那么 快指针的长度是:K+慢在循环圈里的距离+ 满圈的长度*n圈; 慢指针的长度是:K+慢在循环圈里的距离,同时,因为快指针每次走两步,慢指针每次走一步,所以 快的长度等于慢长度的2倍:得到:(K+慢在循环圈里的距离+ 满圈的长度*n圈)=2*(K+慢在循环圈里的距离)所以:
上式子可以变成:K+慢在循环圈里的距离+ 满圈的长度*n圈 =K+慢在循环圈里的距离+K+慢在循环圈里的距离。
所以可以得到:满圈的长度*n圈 =K+慢在循环圈里的距离;
如果n是1, 那么k的长度就是等于现在相遇地方到循环开始的地方。就是k到循环开始的地方和相遇地方到循环开始的地方。。
如果 n是2,那么k到循环开始的地方就是,循环一圈加相遇地方到循环开始的地方。
所以,k到循环开始的地方和相遇地方到循环开始的地方。
所以,一个指针在原地,一个指针在开始的地方,都往前走,相遇的那个点就是循环开始的地方。
public class Circ6 {
static class LinkNode {
int val;
LinkNode next;
LinkNode(int x) {
val = x;
next = null;
}
}
public static LinkNode FindBeginning(LinkNode head) {
LinkNode slowp = head;
LinkNode fastp = head;
while (fastp != null && fastp.next != null) {
fastp = fastp.next.next;
slowp = slowp.next;
if (fastp == slowp) {
break;
}
}
if (fastp == null || fastp.next == null) {
return null;
}
slowp = head;
while (slowp != fastp) {
slowp = slowp.next;
fastp = fastp.next;
}
return slowp;
}
}
网上还看到另一个解题方法:
就是用hashSet,存每一个节点,如果set里存在这个节点,就返回这个节点。
public LinkNode firstNodeInCircle1(LinkNode head) {
if (head == null || head.next == null)
return null;
Set<LinkNode> hashSet = new HashSet<LinkNode>();
while (head != null) {
if (hashSet.contains(head)) {
return head;
} else {
hashSet.add(head);
head = head.next;
}
}
return null;
}
Reference:
http://www.hawstein.com/posts/2.5.html
http://www.jyuan92.com/blog/careercup2_6-first-node-in-circle-linkedlist/
https://github.com/1094401996/CareerCup/blob/master/Chapter02/src/twodot6/Circular.java
cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.的更多相关文章
- CCI_chapter 2 Linked Lists
2.1 Write code to remove duplicates from an unsorted linked list /* Link list node */ struct node { ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 380. Intersection of Two Linked Lists【medium】
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Leetcode 160. Intersection of two linked lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- (LinkedList)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java for LeetCode 160 Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- 【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...
- MySQL查询随机数据的4种方法和性能对比
从MySQL随机选取数据也是我们最常用的一种发发,其最简单的办法就是使用”ORDER BY RAND()”,本文介绍了包括ORDER BY RAND()的4种获取随机数据的方法,并分析了各自的优缺点. ...
- Unity Navigation自动寻路
NavMesh(导航网格) 是3D游戏世界中主动寻路的一种技术,如果你想让游戏人物能自动绕开障碍物到达目的地.那你就来学习下 Navigation导航技术吧O(∩_∩)O~ 首先创建一个项目创建Pan ...
- 8. 冒泡法排序和快速排序(基于openCV)
一.前言 主要讲述冒泡法排序和快速排序的基本流程,并给出代码实现,亲测可用. 二.冒泡法排序 冒泡法排序主要是将相邻两个值比较,把小的向前冒泡,大的向后沉淀,时间复杂度为O(n2).主要思想如下: 分 ...
- 0124——KVC KVO模式
1.KVC KVC是Key-Value-Coding的简称,它是一种可以直接通过字符串的名 字(key)来访问类属性(实例变量)的机制.而不是通过调用Setter.Getter方法访问.当使用KVO. ...
- UILabel自适应高度,自动换行
CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...
- react基于webpack和babel以及es6的项目搭建
项目demo地址https://github.com/aushion/webpack_reac_config 1.打开命令提示窗口,输入 mkdir react_test cd react_test ...
- python 技巧 之 pyCharm快速添加第三方库和插件
学习python有几个月,安装第三方库都是通过 pip install 或者 easy_install.每次都要打开命令行感觉太麻烦.还好Pycharm提供了安装第三方库和安装插件的功能. 首先打开P ...
- QT皮肤框架-TQUI
本皮肤框架的相关文档,请在附件中下载,包括测试程序源码,帮助文档.相关文档可到我的百度网盘中下载,或者在本贴附件中下载. 百度网盘地址:TQUI-V1.0项目说明及测试程序源码 项目更新说明:---- ...
- CDZSC_2015寒假新人(1)——基础 g
Description Ignatius likes to write words in reverse way. Given a single line of text which is writt ...