去Twitter面试的被问到这个问题,当时只想到了用HashMap的办法,这种办法时间复杂度O(n),空间复杂度是O(n), 更好的办法是用 FastRunner / SlowRunner approach。用两个pointer遍历链表,fast的速度是slow的两倍,如果有loop,二者一定会collide。

boolean detectLoop(LinkedListNode head){
LinkedList slow = head;
LinkedList fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
     if(slow == fast){
return true;
}
}
return false;
}

What if we want to find the start of the loop?

Detect loop in a singly linked list的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  2. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  3. Singly Linked List

    Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...

  4. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  5. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  6. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  7. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  8. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  9. Singly linked list algorithm implemented by Java

    Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...

随机推荐

  1. c# 如何处理自定义消息

    C#自定义消息通信往往采用事件驱动的方式实现,但有时候我们不得不采用操作系统的消息通信机制,例如在和底层语言开发的DLL交互时,是比较方便的.下面列举了一些实现方式,供大家参考:一.通过SendMes ...

  2. (转)卸载和安装LINUX上的JDK

    卸载默认的: 用root用户登陆到系统,打开一个终端输入 # rpm -qa|grep gcj 显示内容其中包含下面两行信息 # java-1.4.2-gcj-compat-1.4.2.0-27jpp ...

  3. Spring Mvc模式下Jquery Ajax 与后台交互操作

    1.基本代码 1)后台控制器基本代码 @Controller @RequestMapping("/user") public class UserController { @Aut ...

  4. 函数 sort,unique,stable_sort,count_if,谓词

    bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...

  5. emacs_1

    --> 正在处理依赖关系 perl(VMS::Filespec),它被软件包 perl-PathTools-3.2701-1.el5.rf.x86_64 需要---> 软件包 perl-p ...

  6. JetBRAINS 系列注册机

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/1118/ 说明 这个是一国外的大牛写的一个 JetBRAINS 系列注册机,他里面包含了很多,我就不打字了. ...

  7. Android - 代码片段

    转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/android-code-snippets/ 说明 此篇文章为个人日常使用所整理的一此代码片段,此篇文正将会不 ...

  8. Ubuntu 截屏

    一个图说明: 系统设置->键盘->快捷键->屏幕截图 里面可以查看及修改快捷键

  9. 用C#对ADO.NET数据库完成简单操作

    数据库访问是程序中应用最普遍的部分.随着C#和ADO.NET的引入,这种操作变得更简单.这篇文章将示范四种最基础的数据库操作. ● 读取数据.其中包括多种数据类型:整型,字符串,日期型. ● 写数据. ...

  10. artdialog 提示 确定或取消

    dialog({ title:'提示', content:"下载需扣除" + point + "个积分<br />重复下载不扣积分,需要继续吗?", ...