【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 ...
随机推荐
- Mybatis 学习-4
Category与Article双向一对多关联 (1)将CategoryDao进行实现 public class CategoryDaoImpl extends BaseDao<Category ...
- springMVC 验证器
采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先我们要到http://hibernate.org/val ...
- iOS 中对 HTTPS 证书链的验证
这篇文章是我一边学习证书验证一边记录的内容,稍微整理了下,共扯了三部分内容: HTTPS 简要原理: 数字证书的内容.生成及验证: iOS 上对证书链的验证. HTTPS 概要 HTTPS 是运行在 ...
- jquery iframe高度自适应
$(document).ready(function () { $("#test").load(function () { var thisheight = $(this).con ...
- soapUI参数
点击File->New Rest Project,填入要测试的URI,确定进入编辑界面: 调整请求方式,添加请求参数,设置参数风格,这里要说一下:style有五种,QUERY是默认常用:TEMP ...
- Div样式查看器
编写div属性时,经常需要尝试不同的样式,可以用Javascript写一个简单的div样式查看器,方便日常操作: <!DOCTYPE html> <html> <head ...
- ACTIVITI 源码研究之命令模式执行
ACTIVITI 是一个优秀开源软件,通过阅读源码,我们不但可以了解工作流引擎执行的原理还可以增加个人的编码功力. ACTIVITI 所有执行过程都是采用命令模式进行执行. 本文主要描述流程引擎数据保 ...
- 斐波那契博弈(Fibonacci Nim)
问题: 有一堆个数为n(n>=2)的石子,游戏双方轮流取石子,规则如下: 1)先手不能在第一次把所有的石子取完,至少取1颗: 2)之后每次可以取的石子数至少为1,至多为对手刚取的石子数的2倍. ...
- bzoj 2243: [SDOI2011]染色
#include<cstdio> #include<iostream> #define M 1000006 #define N 1000006 using namespace ...
- warning malformed '#pragma pack(push[, id], n)' - ignored
bmp.c:8: warning: malformed '#pragma pack(push[, id], <n>)' - ignored bmp.c:33: warning: #prag ...