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.的更多相关文章

  1. CCI_chapter 2 Linked Lists

    2.1  Write code to remove duplicates from an unsorted linked list /* Link list node */ struct node { ...

  2. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

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

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

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

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

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

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

随机推荐

  1. UVA 11374 Airport Express(枚举+最短路)

    枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...

  2. RAID,mdadm(笔记)

    RAID: 级别:仅代表磁盘组织方式不同,没有上下之分:0: 条带    性能提升: 读,写    冗余能力(容错能力): 无    空间利用率:nS    至少2块盘1: 镜像    性能表现:写性 ...

  3. pyqt信号事件相关网址说明及python相关

    pyqt在线文档: http://www.rzcucc.com/search/pyqt.sourceforge.net/Docs/PyQt4/-qdatetime-2.html PyQT信号槽_学习笔 ...

  4. Rainmeter 雨滴桌面 主题分享

    说明 先安装主程序 Rainmeter-3.1.exe,然后安装 Techzero_1.0.rmskin,打开主题管理应用主题就可以. 下载 http://pan.baidu.com/s/1i3zI3 ...

  5. Java中的编码格式

    Java中的编码 gbk编码 中文占用2个字节,英文占1个字节; utf-8编码 中文占用3个字节.,英文占用1个字节; Java是双字节编码 (utf-16be) utf -16be 中文占2个字节 ...

  6. SQL 练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  7. 自定义TypeConverter把基础类型转换为复杂类型

    原文(http://tech.it168.com/d/2008-06-30/200806300953554_all.shtml) TypeConverter对于编写ASP.NET Server Con ...

  8. .net DataTable 正确排序姿势

    关于dataTable中根据列排序正确姿势做个随笔,方便查阅 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Ad ...

  9. Javascript的性能瓶颈

    Javascript是单线程的,它的性能瓶颈在于频繁的DOM操作, 因为每次操作都会使浏览器重新绘制一次. 其实纯JS的执行的速度是很快的,可以把元素都攒到一块,一次性放到页面中. 或者,定义一个延时 ...

  10. web.cofing(新手必看)

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...