LeetCode: Linked List Cycle [141]
【题目】
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
【题意】
推断一个单向链表是否有环
【思路】
维护两个指针p1和p2,p1每次向前移动一步,p2每次向前移动两步
假设p2可以追上p1,则说明链表中存在环
【代码】
/**
* 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(p2){
//p1向前走一步
p1=p1->next;
//p2向前走两部
p2=p2->next;
if(p2)p2=p2->next;
//推断p2是否追上了p1
if(p2 && p2==p1)return true;
}
return false;
}
};
LeetCode: Linked List Cycle [141]的更多相关文章
- 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 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [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]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle 解题报告
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode Linked List Cycle 解答程序
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
随机推荐
- [BZOJ5267]特工
一个套路题...但还是得写一下这个套路避免以后忘了 题目中的运算$f(i,j)=(i|j)\text^i$对单位二进制满足$f(0,0)=f(1,0)=f(1,1)=1,f(0,1)=0$ 先考虑求正 ...
- [转] Spring@Autowired注解与自动装配
1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...
- OpenVPN官方资源下载和官方教程入口
官方资源: https://openvpn.net/index.php/open-source/downloads.html 2.4.4版本下载:(链接: https://pan.baidu.com/ ...
- 基于tiny4412的Linux内核移植 -- DM9621NP网卡驱动移植(四)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- ejs循环实例
... //index page var items=[{title:"文章1"},{title:"文章2"}]; app.get('/',function(r ...
- Android内存优化12 内存泄漏常见情况3 注册泄漏
android 中有很多注册和反注册,由于在注册后,上下文自身会被持久化的观察者列表所持有,如果不进行反注册,就会造成内存泄漏 内存泄漏1:Sensor Manager 代码如下: MainActiv ...
- 如何解决weblogic server启动中在IIOP后运行缓慢
WebLogic Server在Linux环境中,有时因为linux OS的安全包没有安装,导致weblogic server 在启动的时候会在长时间的停留在 <2/07/2009 08:54: ...
- 万里长征第二步——django个人博客(第五步 ——配置后台admin)
在urls.py文件中配置admin路径 from django.conf.urls import url from django.contrib import admin from blog.vie ...
- 如何在sublime3项目设置中设置python模块的搜索路径?ImportError: No module named *的解决办法
问题:之前使用pycharm,用的挺溜,但是电脑配置实在不争气,pycharm启动久了,耗去大量内存,导致运行起来越来越慢,于是转向使用sublime text. 把项目从pycharm切换到subl ...
- javascript 中contentWindow和 frames和iframe之间通信
iframe父子兄弟之间通过jquery传值(contentWindow && parent),iframe的调用包括以下几个方面:(调用包含html dom,js全局变量,js方法) ...