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. Tomcat Manager用户配置详解

      Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Ma ...

  2. (04)odoo视图操作

    -----------------更新时间19:04 2016-09-29 星期四11:17 2016-09-18 星期日18:13 2016-04-05 星期二15:05 2016-03-14 星期 ...

  3. uva--1339 - Ancient Cipher(模拟水体系列)

    1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...

  4. JS引用类型之——数组

    前言 数组作为JS中非常常用的引用类型,其功能是非常强大滴,今天小猪就彻底的看了下它.为了防止猪脑子不够用所以记录在案呐 1.数组的创建 var arrayObj = new Array(); //创 ...

  5. [原创]checkstyle下载与安装

    checkstyle是一款功能很强的java静态代码检查工具,为eclipse的插件.在网上看了,大致有两种安装方法.第一种 为联网,在eclipse里输入URL下载:另一种为下载好插件后,离线安装. ...

  6. javascript——拖拽(完整兼容代码)

    拖拽,是JS经常会用到的效果,在网上有很多的这样那样的拖拽效果,但其中往往大多有各种各养的问题,功能不全,无法兼容,而且修改的时候 也是十分麻烦. 其实拖拽的原理很简单,无非是鼠标的三个动作的解析,以 ...

  7. OL/SQL编程练习

    create or replace procedure pr_first is --一个变量 v_a ) := '总有一天我的生命将走到尽头'; --一个常量 c_b constant ) := '而 ...

  8. visual studio 2013连接Oracle 11g并获取数据:(二:实现)

    1.VS中新建一个winform窗体 (1)一个按钮 (2)一个数据表格视图(在里面显示得到的数据表) 2.双击按钮进入代码 (1)添加 using System.Data.OracleClient; ...

  9. async = require('async')

    var mongoose = require('mongoose'), async = require('async'); mongoose.connect('localhost', 'learn-m ...

  10. Java中的深拷贝和浅拷贝

    1.浅拷贝与深拷贝概念 (1)浅拷贝(浅克隆) 浅拷贝又叫浅复制,将对象中的所有字段复制到新的对象(副本)中.其中,值类型字段(java中8中原始类型)的值被复制到副本中后,在副本中的修改不会影响到源 ...