【LeetCode】【Python】Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
思路:笨办法是每一个节点再开辟一个属性存放是否訪问过,这样遍历一遍就可以知道是否有环。但为了不添加额外的空间。能够设置两个指针。一个一次走一步,还有一个一次走两步,假设有环则两个指针一定会再次相遇。反之则不会。
# 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 boolea
def hasCycle(self, head):
if head is None:
return False
p1 = head
p2 = head
while True:
if p1.next is not None:
p1=p1.next.next
p2=p2.next
if p1 is None or p2 is None:
return False
elif p1 == p2:
return True
else:
return False
return False
【LeetCode】【Python】Linked List Cycle的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【ArcGIS遇上Python】ArcGIS Python批处理入门到精通实用教程目录
目录 1. 专栏简介 2. 专栏地址 3. 专栏目录 1. 专栏简介 Python语言是目前很火热的语言,极大的促进了人工智能发展.你知道在ArcGIS中也会有python的身影吗?事实上,在ArcG ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 【LeetCode每天一题】Reverse Linked List(链表反转)
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL ...
- 【leetcode刷题笔记】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刷题笔记】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- ESCAPE用法
ESCAPE用法1.使用 ESCAPE 关键字定义转义符: 在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符. 2.ESCAPE 'escape_character' 允许在字符串中搜索通 ...
- wpf动画概述
http://msdn.microsoft.com/zh-cn/library/vstudio/ms752312(v=vs.100).aspx Windows Presentation Foundat ...
- Android Project from Existing Code 生成 R 文件错误、失败等问题解决办法 - 持续更新
Android Project from Existing Code 生成 R 文件错误.失败等问题解决办法 - 持续更新 git 上的项目,pull下来之后用Android Project fro ...
- Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息
AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module.所谓的Module就是我们通常所指的模块化的一个单元.并经常以jar包的形式存在.以下以一个获取手机信息的样例演 ...
- HTTP Digest authentication
(Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描写叙述.其身份验证机制非常easy,它採用杂凑式(hash ...
- git使用图解
使用前 安装git 配置name 和 email git config --global user.name "Your Name" git config --global use ...
- android 数据存储之SharePreference 的几种方式
1. 常见的 getSharedPreferences(String filename,mode) 指定sp文件的名称,生成的文件名为 filename.xml 2.getPreferences(mo ...
- css中bug记录
1.margin塌陷,通俗叫法. 假如一个盒子box里边嵌套了两个盒子(记为box1,box2).box1的margin-top不会如预想的一样在box顶部撑开一个边距,而是以包含box的容器上边界为 ...
- 全分布式环境下,DataNode不启动的问题解决
问题出现:机器重启之后,再次在master结点上面执行start-all.sh,发现有一个datanode没有启动,通过jps检查之后,发现slave1上面的datanode进程未启动 原因:每次na ...
- sql 练习(2)
1.用Decode函数实现横表和竖表的转换 SELECT deptno DEPARTMENT_ID, COUNT(*) "部门人数", ),,,)) "超高收入人数()& ...