Problem link:

http://oj.leetcode.com/problems/linked-list-cycle/

We set two pointers: the faster pointer goes two steps each iteration, and the slower one goes one step.

If the two pointers meet some time, it follows that there is a loop; otherwise, the faster pointer will touch the end of the singly linked list, and return False.

The algorithm will solve the problem correctly and terminate in linear time. For details, please refere to my homepage doc.

The python code is as follows.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
slow = head
fast = head
while fast is not None:
slow = slow.next
if fast.next is None:
break
else:
fast = fast.next.next
if slow == fast:
return True
return False

【LeetCode OJ】Linked List Cycle的更多相关文章

  1. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

  2. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

  6. 【LeetCode OJ】Convert Sorted List to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...

  7. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  8. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  9. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

随机推荐

  1. iOS视频直播初窥:高仿<喵播APP>

    视频直播初窥 视频直播,可以分为 采集,前处理,编码,传输, 服务器处理,解码,渲染 采集: iOS系统因为软硬件种类不多, 硬件适配性比较好, 所以比较简单. 而Android端市面上机型众多, 要 ...

  2. Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  3. python 循环设计

    for循环 1.range()用法 for循环后的in跟随一个序列的画,循环每次使用的序列元素而不是序列的下标 例:s='abcdefg' for i in range(0,len(s),3): pr ...

  4. nssm在windows服务器上部署nodejs,coffee启动方式

    本想用forever / pm2 来部署nodejs, 百度后发现只能在Linux系统上使用,window上没法使用,兜一圈后又转nssm了.... 在Linux上,可以轻松的使用forever或者p ...

  5. HTML5的 2D SVG和SVG DOM的学习笔记(1)

    (项目中要使用SVG,只好补充知识了) HTML体系中,最常用的绘制矢量图的技术是SVG和HTML5新增加的canvas元素.这两种技术都支持绘制矢量图和光栅图. 一.SVG概述 可缩放矢量图形(Sc ...

  6. vue.js——初体验

    看到最近很火的vue.js,于是开启了自己人生中首篇翻译之路,才意识到这个纯英文版的的确没有中文的通俗易懂~~~~~~不过, 还是硬着头皮把这篇英文版的博客给翻译完了,希望可以帮助自己的同时也方便别人 ...

  7. c++实现之 -- 汉语词语的简单处理

    好了,我们现在已经会怎样读入了,然后就是研究一下如何存储等一些细节上的的问题了. 首先,比较函数是不能传入char*的地址的,但是可以接受一个string类. 然而,如果是两个比较长的string类, ...

  8. 铁人系列(2)LA2218

    思路:对于每个人  都会有n-1个半片面  加上x>0,y>0,1-x-y>0(这里的1抽象为总长) 代码是粘贴的  原来写的不见了  orz............ // LA22 ...

  9. iOS Android图标生成器PHP

    <?php //修改为你想要的大小 //$sizes = array(16,29,32,36,48,50,57,58,72,76,96,100,114,120,128,144,152); $si ...

  10. Unity中的Path对应各平台中的Path

    OS: Application.dataPath :                    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.a ...