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 ...
随机推荐
- set集合综合案例
案例01:生成0-10之间5个不相等的数 方法1:使用list集合实现 import random list01 = [] for i in range(100): num01 = random.ra ...
- 4星|《行为设计学:掌控关键决策》:影响决策质量的四大思维陷阱及WRAP应对法
行为设计学:掌控关键决策 两位作者认为,有四大思维陷阱让人做出错误的决策:思维狭隘.证实倾向.短期情绪.过度自信.两位作者提出WRAP决策流程来应对:Widen your options(拓宽选择空间 ...
- idea本地将本地现有的项目和gitlab进行管理并提交到线上
备注:通过这个操作可以让本地的项目与远程gitlab进行关联,并将本地的代码提交到gitlab上面 1.在idea的菜单项选择 VCS>Import into Version Control&g ...
- SDN第4次上机作业
作业链接 1.建立以下拓扑,并连接上ODL控制器. ODL拓扑界面截图: 安装odl参考链接 2.利用ODL下发流表,使得h3在10s内ping不通h1,10s后恢复. 验证性连通性截图: 将hard ...
- [部署]VM11下CentOS7mini安装及配置
最近使用了CentOS发现比Ubuntu更简洁,有些爱上CentOS了 1. 准备一版CentOS安装镜像文件 官网下载地址:http://www.centos.org/download/ 官方有三个 ...
- STL 1–迭代器std::begin()和std::end()使用
迭代器是一个行为类似于指针的模板类对象.只需要迭代器iter指向一个有效对象,就可以通过使用*iter解引用的方式来获取一个对象的引用.通常会使用一对迭代器来定义一段元素,可以是任意支持迭代器对象的元 ...
- Python2.7-argparse
argparse模块,用于方便地编写用户友好的命令行接口,可以自动生成帮助信息,用法信息和错误处理 1.模块对象 1.1 ArgumentParser 对象 1.1.1 初始化构建类:Argument ...
- Android动态的全屏和退出全屏
转自:http://chroya.iteye.com/blog/974031 让程序全屏的方法,大家都知道,那是静态的,程序运行之初就申明了.但是如果有这样的需求:要在程序运行的过程中,执行了某个操作 ...
- neo4j用collect 代替union 并实行分页
MATCH pa=(j:User)-[r:PostLikeRel|:ReplyRel|:RetweetRel]->(m:User{guid:"f092a1dc6c23b26b020bd ...
- 20155206 Exp2 后门原理与实践
20155206 Exp2 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP 然后使用ncat.exe程序,ncat. ...