[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 extra space?
给定一个链表,判断是否有环存在。Follow up: 不使用额外空间。
解法:双指针,一个慢指针每次走1步,一个快指针每次走2步的,如果有环的话,两个指针肯定会相遇。
Java:
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
Python:
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):
fast, slow = head, head
while fast and fast.next:
fast, slow = fast.next.next, slow.next
if fast is slow:
return True
return False
C++:
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
};
类似题目:
[LeetCode] 142. Linked List Cycle II 链表中的环 II
All LeetCode Questions List 题目汇总
[LeetCode] 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 判断链表是否有环 C++/Java
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 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [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环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- (链表 双指针) 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(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- 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判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
随机推荐
- appium+python自动化63-使用Uiautomator2报错问题解决
前言 appium desktop V1.7.1版本使用命令行版本启动appium后,使用Uiautomator2定位toast信息报错:appium-uiautomator2-server-v0.3 ...
- 记一次用pip安装docker-compose报错及解决方法
Docker-Compose 的安装 方法一 # 下载1.25.0 docker compose sudo curl -L "https://github.com/docker/compos ...
- Spring源码窥探之:扩展原理BeanFactoryPostProcessor
BeanPostPorcessor是在bean创建对象初始化前后进行拦截工作,而BeanFactoryPostProcessor是Bean工厂的后置处理器,在Bean定义加载完成之后,Bean实例初始 ...
- c语言的可变参数实例
可变参数函数实现的步骤如下: 1.在函数中创建一个va_list类型变量 2.使用va_start对其进行初始化 3.使用va_arg访问参数值 4.使用va_end完成清理工作 接下来我们来实现一个 ...
- Spark Partition
分区的意义 Spark RDD 是一种分布式的数据集,由于数据量很大,因此它被切分成不同分区并存储在各个Worker节点的内存中.从而当我们对RDD进行操作时,实际上是对每个分区中的数据并行操作.Sp ...
- ImportError: cannot import name HTTPSHandler
原因在于openssl,openssl-devel两个文件包未正确安装.用下来的命令来安装: 2 yum install openssl -y yum install openssl-devel -y ...
- springboot使用jdbcTemplate案例
1 创建实体类 public class Student { private Integer stuid; private String stuname; public Integer getStui ...
- [ML] Tensorflow.js + Image segmentPerson
<!DOCTYPE html> <html> <head> <title>Parcel Sandbox</title> <meta c ...
- 意图Intent
意图点击官方链接 前言 对意图Intent,学习安卓需掌握.以官方链接:http://www.android-doc.com/reference/android/content/Intent.html ...
- 【POJ1321】棋盘问题
本题传动门 本题知识点:深度优先搜索 + 枚举 + 回溯 题意是要求我们把棋子放在棋盘的'#'上,但不能把两枚棋子放在同一列或者同一行上,问摆好这k枚棋子有多少种情况. 我们可以一行一行地找,当在某一 ...