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 ...
随机推荐
- 1.Dubbo2.5.3源码编译
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.安装JAVA.Git.Maven 安装过程省略,请自行百度. 2.编译dubbo (1)从ht ...
- 常用npm 命令
npm 官方网站:npm的使用说明 安装模块 npm install 安装当前目录package.json文件中配置的dependencies模块 安装本地的模块文件 npm install ...
- docker-compose.md
安装 pip python 2.7+的系统同yum先安装pip命令. # yum install -y python2-pip # pip install docker-compose 网络安装 # ...
- 关于requests.exceptions.SSLError: HTTPSConnectionPool
问题: requests.exceptions.SSLError: HTTPSConnectionPool(host='mall.christine.com.cn', port=443): Max r ...
- [luogu4072] 征途
题面 题意体面中写得很明确, 应该不用我说了, 方差的概念在初中人教版九年级数学中有所提到, 没有上过初中的同学们可以左转百度. 将序列拆为几段求最值, 我们考虑用dp来实现. 先推一下式子, 令方差 ...
- 关于Spring IOC (DI-依赖注入)需要知道的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 <Spring入门经典>这本书无论对于初学者或者有 ...
- $.toJSON和eval的区别
1.$.toJSON是jquery的方法.eval是javascript的方法 2.eval兼容的浏览器多,$.toJSON有可能解析不了的json格式的数据,eval可以.
- 分享一个excel根据文件超链接获取链接文档的最后更新时间
#获取制定单元格内超链接对应的链接地址Sub geturi() For Each cell In Range("E3:E43") If cell.Hyperlinks.Count ...
- WorldWind源码剖析系列:配置载入器类ConfigurationLoader
配置载入器类ConfigurationLoader主要从指定的路径中加载保存星球相关参数的xml文件,从中读取数据来构造星球对象及其所关联的可渲染子对象列表并返回.该类的类图如下所示. 该类所包含的主 ...
- JS 点击元素发ajax请求 打开一个新窗口
JS 点击元素发ajax请求 打开一个新窗口 经常在项目中会碰到这样的需求,点击某个元素后,需要发ajax请求,请求成功以后,开发需要把链接传给前端(或者说请求成功后打开新窗口),前端需要通过新窗口打 ...