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. WEB打印插件Lodop

    Lodop.C-Lodop使用说明及样例   Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又可用程序代码直接实现 复杂打印.控件功能强大,却简单易用,所有调用如 ...

  2. UML建模工具-火龙果软件

     官网地址:http://code.uml.com.cn/index.asp     Bridge桥梁模式    (待逆向) 桥梁模式,通过增加一个类,将抽象部分与它的实现部分分离,使它们都可以独立 ...

  3. GIT入门篇-基本概念与操作

    GIT 首先必须说明的是, 这篇文章不是阐述GIT原理性和比较深入的文章.只是对于日常开发中比较常用的需求的总结和GIT这些命令大体的原理解释.所以掌握这个只能说能够应付一定的开发需求.但是如果你是个 ...

  4. POJ-2955括号匹配问题(区间DP)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4834   Accepted: 2574 Descript ...

  5. [Redux] Extracting Action Creators

    We will create an anction creator to manage the dispatch actions, to keep code maintainable and self ...

  6. Rss 的作用 及使用方法

    也可以参考http://jingyan.baidu.com/article/e73e26c0c73e1f24adb6a70f.html 什么是RSS RSS是站点用来和其他站点之间共享内容的一种简易方 ...

  7. js校验

    判空 function check(s) { return (s == null || typeof (s) == "undefined" || s == "" ...

  8. oracle 临时表空间的增删改查

    oracle 临时表空间的增删改查 oracle 临时表空间的增删改查 1.查看临时表空间 (dba_temp_files视图)(v_$tempfile视图)select tablespace_nam ...

  9. css_day5

  10. My way to Python - Day03

    列表和字典的赋值 dict1 = {} dict1['k1'] = 'v1' list1 = [] list1.append('v1') 集合系列 1,计数器 Python 2.7.6 (defaul ...