[LeetCode]题解(python):141-Linked List Cycle
题目来源:
https://leetcode.com/problems/linked-list-cycle/
题意分析:
给定一个链表,判断链表是否有环。要求O(1)空间时间复杂度。
题目思路:
用快慢指针可以解决这个问题。一个指针每次走两步,一个每次走一步,那么有环的等价条件是两个指针有重合。通过快慢指针还可以全环的长度。
代码(python):
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head == None or head.next == None:
return False
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
[LeetCode]题解(python):141-Linked List Cycle的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 141. Linked List Cycle - LeetCode
Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...
- 【LeetCode题解】链表Linked List
1. 链表 数组是一种顺序表,index与value之间是一种顺序映射,以\(O(1)\)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后续的数据元素进行移动,复杂度 ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- [LeetCode] 141. Linked List Cycle 链表中的环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
随机推荐
- Temporary Post Used For Theme Detection (19f70e1d-5d8d-4c19-aef1-5b5a71ae0c47 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
This is a temporary post that was not deleted. Please delete this manually. (90462755-633b-4a0e-abb7 ...
- php使用正则
$str = '2';echo preg_match('/^\d{1,10}$/i',$str);//匹配数字,最少1位,最多10位,返回1,否则返回0;
- 【转载】VS2010+VMWare8+VisualDDK1.5.6 创建并调试驱动程序 - 完全教程
原帖:http://techird.blog.163.com/blog/static/1215640362011112385241568/ 本文描述了如何使用Visual Studio+VMMare+ ...
- mysql 索引创建规则
1.表的主键.外键必须有索引:2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: 5 ...
- Android studio听云接入另外一种方式
1.在build.gradle中集成听云. buildscript { repositories { mavenCentral() } dependencies { classpath fileTre ...
- nodejs 文件查找中文,替换为英文
帮以前同事解决一个需求,中文项目 翻译 英文项目~~~ 考虑到具体实现方面的问题,如果智能的话,肯定是要做中文的语法分析,不过感觉这个有难度. 所以最后的方案是遍历文件,将中文短语匹配出来,再进行人工 ...
- C#学习日志 day 5 plus------ interface 数组及stringBuilder相关
interface 接口interface可以理解为两个程序达成的协议. 实际就是一个留给后续开发的框架.若想继承这个interface,就必须实现interface规定的 函数及结构等.一般会以大写 ...
- lua学习笔记1
lua中调用c的函数 #include <stdio.h> #include <string.h> #ifdef __cplusplus extern "C" ...
- U盘常见故障及检修
一般U盘故障分为软故障和硬故障,其中以软故障最为常见. 软故障主要是指U盘有坏块,从而导致U盘能被计算机识别,但没有盘符出现,或者有盘符出现,但当打开U盘时却提示要进行格式化,而格式化又不能成功.前 ...
- POJ1961Period
POJ1961 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...