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 ...
随机推荐
- 对复杂字典Dictionary<T1,T2>排序问题
原文:对复杂字典Dictionary<T1,T2>排序问题 //VoltageCount类(电压值对应的数量): public class VoltageCount { ...
- mvn 命令在command prompt无法识别
Download maven from this website: https://maven.apache.org/download.cgi 解压binary包后放到一个位置,比如C:\apache ...
- Android微信支付SDK
App对接微信调起微信支付需要在微信平台注册,鉴别的标识就是App的包名,所以将申请的包名单独打包成一个Apk文件,则在其他的App调起此Apk的时候同样可以起到调用微信支付的功能.这样就实现了调起微 ...
- C++得到当前进程所占用的内存
原文地址:C++得到当前进程所占用的内存作者:雪碧狗 使用SDK的PSAPI (Process Status Helper)中的BOOL GetProcessMemoryInfo( HANDLE P ...
- C#获取带汉字的字符串长度
正常情况下,我们是直接去string的length的,但是汉字是有两个字节的,所以直接用length是错的.如下图: 所以应该用以下代码来获取长度: private void button1_Clic ...
- 数据库连接池之_DButils
// 这个是在添加数据 @Test public void demo1() { QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); ...
- 关于git远程分支操作
对于用户来说,git给人提交到本地的机会.我们可以在自己的机器上创建不同的branch,来测试和存放不同的代码. 对于代码管理员而言,git有许多优良的特性.管理着不同的分支,同一套源代码可以出不一样 ...
- 中芯国际在CSTIC上悉数追赶国际先进水平的布局
作为中国最大.工艺最先进的晶圆厂,中芯国际的一举一动都会受到大家的关注.在由SEMI主办的2017’中国国际半导体技术大会(CSTIC 2017)上,中芯国际CEO邱慈云博士给我们带来了最新的介绍. ...
- Java实现Qt的SIGNAL-SLOT机制(保存到Map中,从而将它们关联起来,收到信号进行解析,最后反射调用)
SIGNAL-SLOT是Qt的一大特色,使用起来十分方便.在传统的AWT和Swing编程中,我们都是为要在 监听的对象上添加Listener监听器.被监听对象中保存有Listener的列表,当相关事件 ...
- ZooKeeper+Dubbo+SpringBoot 微服务Demo搭建
1. 首先创建springBoot项目,springBoot是一堆组件的集合,在pom文件中对需要的组件进行配置.生成如下目录结构 创建test项目,同步在test创建dubbo-api,dubbo- ...