【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* p1 = head;//慢指针
ListNode* p2 = head;//快指针
while(p1!=NULL&&p2!=NULL&&p2->next!=NULL)//如果没有环,总会指向NULL
{
p1=p1->next;
p2=p2->next->next;
if(p1==p2) return true;//慢指针追上快指针,表示成环
}
return false;
}
};
【一天一道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 、 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. 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. To represent a cycle in the given linked lis ...
- 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 Hash fast and slow pointer
Problem describe:https://leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if ...
- 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 ...
- Java for 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 ex ...
- leetcode 141. Linked List Cycle ----- java
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- Python [Leetcode 141]Linked List Cycle
题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...
随机推荐
- 根据构建类型自动修改依赖库的BuildConfig.DEBUG的值
app模块引用了library,在library模块中控制日志输出使用的是 if (BuildConfig.DEBUG) { logger.d("print %s", msg); ...
- win10下python环境变量设置
我用的是python_2.7.3.msi,从官网下载之后,一路按照默认进行安装. 安装之后配置环境变量的步骤如下: 1,点“我的电脑”,右键选“属性” 2,选择“高级系统设置”--->选“环境变 ...
- Chinese-Text-Classification:Tensorflow CNN 模型实现的中文文本分类器[不分词版]
从现在的结果来看,分词的版本准确率稍微高一点. 训练过程: 模型评估: 实验三,准备换一下数据集,用这里的数据集来跑这个模型:https://zhuanlan.zhihu.com/p/30736422 ...
- js延迟函数
正确写法: setTimeout(function (){ alert("delay!"); },5000); 错误写法: setTimeout( alert("dela ...
- Firebird数据库相关操作
Firebird常用SQL 一.分页写法小例: 1 select first 10 templateid,code,name from template ; 2 select first 10 ski ...
- Linux系统中安装Oracle过程记录
第一章 安装数据库软件 1.1 修改密码及创建目录和权限 创建oracle用户和组 创建相关目录并赋权 1.2 设置oracle用户环境变量 ORACLE_BASE:产品基目录 ORACLE_HOME ...
- 解决使用web开发手机网页关于分辨率被缩放的坑
问题的产生 因为各方面原因,要用网页做界面,开发一个APP.内核使用的是腾讯的x5内核. 把外壳交给前端和设计测试的时候,都汇报:状态栏的颜色太不搭配了,要求可修改 遂启用了安卓4.4版本开始支持的沉 ...
- Vue 波纹按钮组件
代码链接:https://github.com/zhangKunUserGit/vue-component 效果图: 大家可以在线运行: https://zhangkunusergit.github. ...
- Codeforces Round #396(Div. 2) A. Mahmoud and Longest Uncommon Subsequence
[题意概述] 找两个字符串的最长不公共子串. [题目分析] 两个字符串的最长不公共子串就应该是其中一个字符串本身,那么判断两个字符串是否相等,如果相等,那么肯定没有公共子串,输出"-1&qu ...
- linux:关于Linux系统中 CPU Memory IO Network的性能监测
我们知道:系统优化是一项复杂.繁琐.长期的工作.通常监测的子系统有以下这些:CPUMemoryIO Network 下面是常用的监测工具 Linux 系统包括很多子系统(包括刚刚介绍的CPU,Memo ...