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 ...
随机推荐
- kudu基础入门
1.kudu介绍 1.1 背景介绍 在KUDU之前,大数据主要以两种方式存储: (1)静态数据: 以 HDFS 引擎作为存储引擎,适用于高吞吐量的离线大数据分析场景.这类存储的局限性是数据无法进行随机 ...
- JS中的防抖与节流
什么是防抖?and什么是节流?一起来开心的学习下吧. 首先什么是防抖:就是在一定的时间内事件只发生一次,比如你点击button按钮,1秒内任你单身30年手速点击无数次,他也还是只触发一次.举个例子,当 ...
- HTTP协议详解之url与会话管理
1 当我们访问一个网址的时候,这中间发生了什么 输入网址——浏览器查找域名的IP地址——浏览器给Web服务器发送一个HTTP请求——服务端处理请—— 服务端发回一个HTTP响应——浏览器渲染显示HTM ...
- thinkphp5整合 gatewaywork实现聊天
1:将下载的gatewaywork下的\vendor 下的workman文件夹,整个复制到tp5下的vendor目录下 2:tp5\application\push 新键push文件夹,将下载的 ...
- [luogu4072] 征途
题面 题意体面中写得很明确, 应该不用我说了, 方差的概念在初中人教版九年级数学中有所提到, 没有上过初中的同学们可以左转百度. 将序列拆为几段求最值, 我们考虑用dp来实现. 先推一下式子, 令方差 ...
- docker swarm英文文档学习-3-开始
https://docs.docker.com/engine/swarm/swarm-tutorial/ 1)Getting started with swarm mode 本教程向你介绍Docker ...
- apache-httpd工作模式
[root@app1 conf]# ../bin/apachectl -lCompiled in modules: core.c mod_access.c mod_auth.c mod_include ...
- SQL 字符串分割表函数
--字符串分割表函数 ) ) declare @i int; declare @count int; ); ); declare @Index int; )) declare @rowID int; ...
- android强制关闭软键盘代码
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.t ...
- CAN总线学习系列之三——CAN控制器的选择
CAN总线学习系列之三——CAN控制器的选择 在进行CAN总线开发前,首先要选择好CAN总线控制器.下面就比较一些控制器的特点. 一些主要的CAN总线器件产品 制造商 产品型号 器件功能及特点 Int ...