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 ...
随机推荐
- Mysql 创建用户并对其赋予操作权限
授权命令GRANT 语句的语法如下: GRANT privileges (columns) ON what TO user IDENTIFIEDBY "password" WITH ...
- Spark机器学习笔记一
Spark机器学习库现支持两种接口的API:RDD-based和DataFrame-based,Spark官方网站上说,RDD-based APIs在2.0后进入维护模式,主要的机器学习API是spa ...
- C++私有构造函数
一. 类的构造函数一般是public的,但是也可以是private的.构造函数为私有的类有这样的特点: <1>不能实例化:因为实例化时类外部无法访问其内部的私有的构造函数: <2&g ...
- linux下的守护进程及会话、进程组
守护进程.会话.进程组网上有许多不错的资料.我也是网上搜罗了一堆,加上自己的理解.不敢说原创,只是写在这怕自己忘记罢了.才疏学浅,难免有错误,欢迎大家指正.下面这篇写很不错,大家可以去看看:http: ...
- 华为測试 字符串运用-password截取
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的password进行通信,比方像这些ABBA.ABA,A,123321,可是他们有时会在開始或结束时增加一些无关的字符以防止别国破解.比 ...
- ssh公私钥登录方式设置
在Linux中ssh登录远程主机的时候能够进行公私钥的认证方式. ①环境说明:两台Linux主机,host1:192.168.5.1,host2:192.168.5.10. 如今在host1上面设置然 ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- 十一招解决:系统IE部分网页打不开怎么办(转载)
网页打不开这问题,却实非常令人头痛,问过非常多人,都说不出真正的理由和解决方法.以下是在网络上面搜集的一些针对“网页打不开怎么办”解决方法,共十一条,希望可以对大家有帮助. Application M ...
- IOS 设备信息读取
let infoDictionary = NSBundle.mainBundle().infoDictionary let appDisplayName: AnyObject? = infoDicti ...
- map的类型映射
以下是使用STL中map类型,对类型的转换示例,主要可以解决的问题,也就是一般的类型之间的相互转换,可以较好的解决相关的问题. 以下是C++源码,比较简短,容易理解的. #include " ...