[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 ...
随机推荐
- Laxcus大数据管理系统2.0(4)- 第一章 基础概述 1.3 节点
1.3 节点 按照我们给Laxcus集群的设计定义,Laxcus集群被分为内部和外部两个网络环境.内部网络由集群的所有权人负责实施和管理,为保证集群能够有效可靠运行,需要遵守一系列的集群部署和管理规定 ...
- Oracle 11g 审计跟踪管理
在Oracle11g之前,oracle数据库自带的审计功能是关闭的,考虑到性能和审计管理的复杂性,用户一般不打开审计功能.如果有审计要求,DBA会采用trigger来实现对DDL审计的方法来折中.例如 ...
- ubuntu中tensorflow安装
首先加入了CUDA_HOM环境变量,然后下载了一个分析工具库,结果E: 无法定位软件包 libcupti-dev,在etc/apt 的sources.list 添加镜像源 deb http://arc ...
- [PocketFlow]解决TensorFLow在COCO数据集上训练挂起无输出的bug
1. 引言 因项目要求,需要在PocketFlow中添加一套PeleeNet-SSD和COCO的API,具体为在datasets文件夹下添加coco_dataset.py, 在nets下添加pelee ...
- DM8168通过GPMC接口与FPGA高速数据通信实现
硬件:TI达芬奇TMS320DM8168(以下简称DSP).EP4CE6E22C8N(以下简称FPGA) 软件:linux-2.6.37 转载请注明出处- http://www.cnblogs.com ...
- 最短路径——Floyd算法(含证明)
通过dij,ford,spfa等算法可以快速的得到单源点的最短路径,如果想要得到图中任意两点之间的最短路径,当然可以选择做n遍的dij或是ford,但还有一个思维量较小的选择,就是floyd算法. 多 ...
- Linux之JDK在线安装及配置
1.查找java相关得列表 yum -y list java*2.在线安装 yum -y install java-1.6.0-openjdk*3.查看安装目录 ls -l /usr/lib/jv ...
- 【Python】PYTHON九九乘法表
python2.7 for i in range(1,10): for j in range(1,i+1): print j,'x',i,'=',j*i,'\t', print '\n'pr ...
- 【bzoj4070】[Apio2015]雅加达的摩天楼 set+堆优化Dijkstra
题目描述 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 “doge” 的神秘生物 ...
- 【心情】HNOI2018游记
Day 0. 全机房的人好像都在做题.然而下午是社团节的游园会,身为社干的我风风雨雨在外面各种搬凳子搬椅子换场地招待外校同学……就这样我好像什么都没有复习. 晚上就一起去酒店了.大概因为是高一的缘故, ...