【一天一道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 ...
随机推荐
- Mybatis Generator 代码生成配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- substr和substring的区别
substr和substring两个都是截取字符串的. 两者有相同点,如果只是写一个参数,两者的作用都是一样的:就是截取字符串当前下标以后直到字符串最后的字符串片段. 例如:`var a=”abcde ...
- RESTful Console Application
RESTful Console Application Introduction Inspirited by RESTFul architecture, A console application t ...
- 两个对象用equals方法比较为true,它们的Hashcode值相同吗?
两个对象用equals方法比较为true,它们的Hashcode值相同吗? 答:不一定相同.正常情况下,因为equals()方法比较的就是对象在内存中的值,如果值相同,那么Hashcode值也应该相同 ...
- linux上快速搭建宝塔面板来操作便捷功能
关于宝塔 Linux版请确保纯净系统安装(支持CentOS.Ubuntu.Debian.Fedora.Deepin),Web端管理,QQ群及论坛技术支持一键LAMP/LNMP.创建网站FTP.数据库. ...
- MultiTigger 绑定异常处理
异常产生环境: 在初始化一个窗口后,没有show出来.在此窗口中,有个控件,重写了控件模板,并加了MultiTrigger. 注意:俩个Condition,一个是从外面绑定过来的Tag,一个是Cont ...
- 47. Permutations II(medium, backtrack, 重要, 条件较难思考)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- C# get 、set、索引器
get 与 set C#类的属性有公有属性(public)和私有属性(private).如果直接将一个属性声明为public,则该类的任意实例可以随意获取或修改该属性的值,很不安全..NET Fram ...
- Python中迭代输出(index,value)的几种方法
需求如下:迭代输出序列的索引(index)和索引值(value). 1.创建测试列表: >>> lst = [1,2,3,4,5] 2.实现方法如下: #方法1:range()+le ...
- 安利三款提升幸福感的chrome插件
谷歌访问助手 chrome浏览器一直是各大码农推荐的比较好用的浏览器,速度快.插件多. 但是由于众所周知的原因导致了谷歌账号同步.扩展商店访问慢甚至打不开的情况. 谷歌访问助手就是用来解决此问题的. ...