[Leetcode Week6]Linked List Cycle
Linked List Cycle 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/linked-list-cycle/description/
Description
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Solution
class Solution {
public:
bool hasCycle(ListNode *head) {
if (head == NULL)
return false;
ListNode *slow = head, *fast = head;
slow = slow -> next;
fast = fast -> next;
if (fast == NULL || fast -> next == NULL)
return false;
else
fast = fast -> next;
while (slow != fast && slow != NULL && fast != NULL) {
slow = slow -> next;
fast = fast -> next;
if (fast == NULL || fast -> next == NULL)
return false;
else
fast = fast -> next;
}
return slow == fast;
}
};
解题描述
这道题是要检查链表是否有环。通过设立两个游标,都从head开始,一个每次往后移动一个节点,一个每次往后移动两个节点。如果两个游标相遇则说明有环。这个问题借用网上的说法就是,在操场上有两个人在跑步,一个人速度是另一个人的两倍,那这两个人肯定会相遇。
[Leetcode Week6]Linked List Cycle的更多相关文章
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [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】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- Selenium搭配TestNG
用Maven来构建TestNG依赖: <dependency> <groupId>org.testng</groupId> <artifactId>te ...
- Fiddler 4 实现手机App的抓包
Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook ...
- 常用模块(xml)
XML(可扩展性标记语言)是一种非常常用的文件类型,主要用于存储和传输数据.在编程中,对XML的操作也非常常见. 本文根据python库文档中的xml.etree.ElementTree类来进行介绍X ...
- 可以完成99%的静态页面的HTML标签
HTML:一套浏览器认知的规则HTML分为两个部分,头和身体.一个完整的网页相当于一个裸体的人,我们利用HTML给它穿上衣服,使它更好看.下面我将为大家介绍一下HTML一些基本的标签,而这些基本的标签 ...
- libvirt保持虚拟机运行情况下修改名称
通过virsh list命令能看到虚拟机的列表: [root@compute2 ~]# virsh list Id 名称 状态 ------------------------------------ ...
- 如何在指定文件夹下进入jupyter notebook
第一步: 打开 Anaconda Prompt 第二步: 查看文件夹所在路径 例如:你有个jupyterwork文件夹在 D:\ 路径下 第三步: 在Anaconda Prompt依次输入一下命令: ...
- Queue模块初识
Queue模块实现了多生产者.多消费者队列.它特别适用于信息必须在多个线程间安全地交换的多线程程序中.这个模块中的Queue类实现了所有必须的锁语义.它依赖于Python中线程支持的可用性:参见thr ...
- android:保存用户名密码等应用程序数据
转自http://blog.sina.com.cn/s/blog_a73687bc0101dsjj.html (一)使用SharedPreferences 1.保存信息: SharedPrefere ...
- Mybatis实例教程整体说明
什么是mybatisMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML或 ...
- 数据结构14——AC自动机
一.相关介绍 知识要求 字典树Trie KMP算法 AC自动机 多模式串的字符匹配算法(KMP是单模式串的字符匹配算法) 单模式串问题&多模式串问题 单模就是给你一个模式串,问你这个模式串是否 ...