【LeetCode OJ】Linked List Cycle
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的更多相关文章
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- 【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 ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【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 ...
- 【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 ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
随机推荐
- datagridview 中添加了一个button类型的列,怎么写button的事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 给文本标签UILabel添加长按复制功能
http://www.111cn.net/sj/iOS/104236.htm http://blog.csdn.net/lrenjun/article/details/12582927 自定义一个可复 ...
- 集成 WeChat SDK - 支付篇
作者感言 惯例, 开头都是要说些东西的了, 随着现在的App越来越商业化, 很多公司的App都会集成第三方的支付SDK, 这样子的成本就比较低, 但是呢, 有很多朋友还是不太会集成, 也不太爱看集成文 ...
- mvc伪静态<三> IIS配置
上一篇已经已经讲述了mvc伪静态的代码实现. 下面以IIS 7.5为例演示一下IIS如何配置才能在服务器显示.html的伪静态 一.进入IIS,选择处理程序映射 二添加脚本映射 三根据你的处理程序的版 ...
- SAP资产明细报表
前两年别人写的,无自定义表字段...直接使用: *&---------------------------------------------------------------------* ...
- MATLAB时间序列预测Prediction of time series with NAR neural network
具体请参考:http://lab.fs.uni-lj.si/lasin/wp/IMIT_files/neural/nn05_narnet/ 神经网络预测时间序列数据,有三种模型, 这里是给出的是第二种 ...
- 使用Yeoman,Grunt和Bower开发AngularJS(译)
使用Yeoman产生AngularJS的主要骨架 使用Grunt加速开发和帮助执行 使用Bower来加入第三方插件和框架——third party plugins/frameworks 一.准备工作 ...
- Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示
本例以将 字符串格式的时间转成指定的时间格式显示. 第一步.定义一个标签处理程序类,需要集成javax.servlet.jsp.tagext.TagSupport,代码如下: import java. ...
- 如何获得bin/Debug目录的路径?
这个可以使用下面的方法 答案:bin目录:通过应用程序集合AppDomain.CurrentDomain.BaseDirectory:带有"\"System.Environment ...
- 有关C#中使用if else和try catch的问题及效率问题
本来if esle 是流程控制 try catch是异常处理,本身其实是没有可比性的,但是开发过程中有的人很容易混用,而且两者有的时候效果似乎一样,所以我还是用了个简单的测试来简单的比较下. 不多说, ...