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 ...
随机推荐
- 一次gcc编译失败分析
1. 场景: 在使用gcc编译时,编译器报错:xxx:'xxx'未定义的引用 2. 排查过程: 首先,我很自然的想到是不是自己使用了未定义的函数或者将函数名称写错了,在检查了一边过后,我发现自己使 ...
- CSRF攻击详解
CSRF是什么 CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSRF/X ...
- CSS3 新增的文本属性
一.CSS1&2中的文本属性(W3C标准) text-indent CSS1 检索或设置对象中的文本的缩进 letter-spacing CSS1 检索或设置对象中的文字之间的间隔 word- ...
- Jenkins Extended E-mail Notification 2个注意事项:
1.Use SMTP AUTHentication 下 user name 所定义的邮箱,必须和全局配置的管理邮箱一直,否则会报错: Error sending to the following VA ...
- WorldWind源码剖析系列:设置类SettingsBase
PluginSDK中的星球设置类WorldSettings 和WorldWind.程序设置类WorldWindSettings均继承自父类SettingsBase.类图如下所示.其中父类Setting ...
- zookeeper&acticemq&redis&tomcat安装
zookeeper安装 配置hosts 下载 wget http://apache.fayea.com/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar. ...
- JAVA框架 Spring AOP底层原理
一:AOP(Aspect Oriented Programming)面向切面编程. 底层实现原理是java的动态代理:1.jdk的动态代理.2.spring的cglib代理. jdk的动态代理需要被代 ...
- Mac OS 上配置java开发环境
在开始本学期的java课程前,我需要先为自己的电脑配置好Java的开发环境.由于电脑是mac操作系统,所以教材上的教程对我并不管用,于是乎开始动手自己查阅网上资料来解决. 1.安装JDK 1.访问Or ...
- Beautiful Soup (一)
一.Beautiful Soup库的理解 1.Beautiful Soup库可以说是对HTML进行解析.遍历.维护“标签树”的功能库 2.pip install bs4 3.from bs4 impo ...
- 服务器端数据合法性验证:签名sign和口令token原理
有时候,你也许会想: 我写的接口,那别人要是知道url,并且知道其需要的数据结构和逻辑,那不是都可以访问了? 甚至是,客户点传递过来的数据,是不是被恶意修改了? 这时,我们可能需要“验证”一下.比如: ...