leetcode 141 Linked List Cycle Hash fast and slow pointer
Problem describe:https://leetcode.com/problems/linked-list-cycle/
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 (-indexed) in the linked list where tail connects to. If pos is -, then there is no cycle in the linked list. Example : Input: head = [,,,-], pos =
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node. Example : Input: head = [,], pos =
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node. Example : Input: head = [], pos = -
Output: false
Explanation: There is no cycle in the linked list. Follow up: Can you solve it using O() (i.e. constant) memory?
Ac Code: (Hash)
/**
* 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) {
unordered_set<ListNode*> visit;
while(head)
{
if(visit.count(head)!=) return true;
visit.insert(head);
head = head->next;
}
return false;
}
};
Fast and Slow Pointer
/**
* 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 *slow = head;
ListNode *fast = head;
while(fast){
if(!fast->next) return false;
fast = fast->next->next;
slow = slow->next;
if(slow == fast) return true;
}
return false;
}
};
leetcode 141 Linked List Cycle Hash fast and slow pointer的更多相关文章
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked 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 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [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 (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 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 ...
- Java for 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 ex ...
- Python [Leetcode 141]Linked List Cycle
题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...
- 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 ...
随机推荐
- 数据绑定(三)为Binding指定绑定源的几种方法
原文:数据绑定(三)为Binding指定绑定源的几种方法 Binding的源是数据的来源,所以,只要一个对象包含数据并能通过属性把数据暴露出来,它就能当作Binding的源来使用,常用的办法有: 一. ...
- Win10如何设置开机自动登录
原文:Win10如何设置开机自动登录 第一步: 小娜搜索"netplwiz",进入用户账户设置. 第二步: 先勾选选中一次,要使用本计算机,用户必须输入用户名和密码. 第三步: 取 ...
- MySQL数据库MHA+keepalive实现
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀 ...
- Linux可以把vmdk当做磁盘加载进去
VMware虚拟机由于停电,无法启动MAC系统 下午的时候,正在开发ios app,结果停电了.当时还不知道,伴随而来的灾难竟然折腾了好几天,真是心力交瘁. 我是在VMware虚拟机下装的Mac os ...
- Docker Explanation and Apache Image
https://blog.sajjan.com.np/2017/02/05/docker-getting-started-containers-ubuntu/ https://blog.sajjan. ...
- 编译icu库(用到了cygwin)
源码下载 icu项目地址 安装cygwin,至少安装以下几个工具 make dos2unix binutils 编译工程 打开命令行,进入根目录的 source 文件夹 配置VC编译环境,执行命令 “ ...
- MASM 命令行编译方法
假设有一个t est.asm ,一个test.rc 可以在CMD里这么编译: ml /c /coff test.asm rc test.rc link /subsystem:windows test. ...
- HTML连载13-CSS基本格式以及文字相关的属性
一.CSS格式 1.注意点: (1)style标签必须写在head标签的开始标签和结束标签之间(也就是必须和title标签师兄弟关系) (2)style标签中的属性type属性其实可以不写,默认就是t ...
- String的所有方法以及解释
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- 【转】php7对redis的扩展及redis主从搭建
一:redis安装 1:下载并安装 cd /home/software wget http://download.redis.io/releases/redis-3.2.3.tar.gz ta ...