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 list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
分析:
给定一个链表,判断链表中是否有环。
遍历链表将节点存在map中,每次添加时,都判断下,是否已经存在。
还可以用快慢指针,慢指针一次走一个,快指针一次走两个,如果链表存在环的话,快慢指针终会相等。
程序:
/**
* 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) {
map<ListNode*, int> m;
while(head){
if(m[head] == )
return true;
m[head] = ;
head = head->next;
}
return false;
}
};
/**
* 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 = head;
ListNode* slow = head;
while(fast && slow){
fast = fast->next;
slow = slow->next;
if(fast){
fast = fast->next;
}
else
break;
if(fast == slow)
return true;
}
return false;
}
};
LeetCode 141. Linked List Cycle环形链表 (C++)的更多相关文章
- [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 判断链表是否有环 C++/Java
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】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...
- 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 ...
- 141 Linked List Cycle 环形链表
给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ...
随机推荐
- 每年有20万人进军IT行业,为何还会人才短缺?
众所周知,IT行业是个高薪行业,也是很多人的梦想职业,在全球最缺人的十大行业中IT行业居首位. 但是现在很多人都有一个疑问: 几乎每所大学里都有计算机技术相关专业,再加上IT培训机构的输出,每年培养出 ...
- BBS论坛博客系统
目录 BBS网站需求分析 BBS数据库设计 BBS用户登录 BBS用户注册 BBS网站首页 BBS个人首页 后台管理系统搭建 网站全部源码
- Use MusicBrainz in iOS(三)查询专辑的完整信息
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010962810/article/details/24066737 本文讨论下通过专辑名获取专辑的 ...
- Netty入门(一)环境搭建及使用
一.项目创建 在 Eclipse 中右键,新建->项目->Maven->Maven Project->下一步->选择 quickstart 下一步->设置如图(参数 ...
- 关于Netty Pipeline中Handler的执行顺序问题
原文地址:http://blog.csdn.net/wgyvip/article/details/25637651 最近在学习Netty框架,根据官网的教程学着做了几个小测试,都成功了,后面开始试着写 ...
- windows下mysql和linux下mysql主从配置
1. linux下mysql安装版本5.6 windows下mysql版本5.7 不要问我为什么版本不一致 就是想这么搞 2. linux为主服务器 windows为从服务器 3.找到li ...
- bip39
BIP: 39 (助记词) Layer: Applications Title: Mnemonic code for generating deterministic keys Author: Mar ...
- 开源http协议库curl和wget的区别和使用
curl和wget基础功能有诸多重叠,如下载等. 在高级用途上的curl由于可自定义各种请求参数所以长于模拟web请求,用于测试网页交互(浏览器):wget由于支持ftp和Recursive所以长于下 ...
- C#自定义控件开发
自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...
- OpenCV——积分图计算
#include <opencv2/opencv.hpp> #include <iostream> #include "math.h" using name ...