【LeetCode】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题。
题目要求:
给定一个链表,判断链表中是否有环。
进阶:
你能否不使用额外空间解决此题?
简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast,一个slow。fast每一步前进两个节点,slow前进一个节点。判断fast和slow是否相等或者为空就行了。
贴个代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast,*slow;
if(head==NULL||head->next==NULL)
return false;
fast=head->next;
slow=head;
while(fast!=slow){
if(fast->next!=NULL&&fast->next->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
else
return false;
}
return true;
}
};
贴个结果:

个人总结:
我最初的想法就是一个动指针一个定指针来循环判断是否为环,这个方法在时间上要比我之前的快。
【LeetCode】Linked List Cycle(环形链表)的更多相关文章
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- 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] 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 ...
- 141 Linked List Cycle 环形链表
给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...
- LeetCode Linked List Cycle 单链表环
题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- tomcat在idea中启动乱码
server,localhost log,catalina log分别乱码 打开tomcat/conf/目录下修改logging.properties 找到"utf-8"行更改为 ...
- ABC时间管理法
名称 ABC时间管理法 属于 事务优先顺序法的“鼻祖” 做法 将待办的事项按照又重要到轻的顺序划分为A,B,C三个等级,然后按照事项的重要等级依据完成任务的做事方法. 特点 使学习.工作和生活等活动在 ...
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...
- C++拾遗(四)——顺序容器
之前一篇博文(<初窥标准库>)简单了解了一种最常用的顺序容器:vector类型.本文将对该文内容进行进一步的学习和完善,继续讨论标准库提供的顺序容器类型.所谓顺序容器,即将单一类型的元素聚 ...
- _T(x) _TEXT(x) L 代表什么?
首先 <tchar.h>中 #ifdef _UNICODE .... #define __T(x) L ## x //替换 #else /* ndef _UNICODE ...
- SQL2005中使用backup、restore来备份和恢复数据库
在SQL2005数据库中利用SQL语句进行数据备份与还原: 备份backup:backup database 数据库名称 tO disk = 备份路径例:BACKUP DATABASE test TO ...
- SQLite C/C++ 教程
目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...
- java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...
- 转向ARC的说明
转自hherima的博客原文:Transitioning to ARC Release Notes(苹果官方文档) ARC是一个编译器特征,它提供了对OC对象自动管理内存.ARC让开发者专注于感兴趣的 ...
- webpack之postcss集成
项目 为了 兼容各个浏览器,需要加各种 c3前缀,如果手动的加肯定 相对比较麻烦,但是现在有webpack,gulp之类的 工具可以自动给我们加上,可以说效率上加速不少.如果 配置中 做个happyp ...