Problem describe:https://leetcode.com/problems/linked-list-cycle/

  1. Given a linked list, determine if it has a cycle in it.
  2.  
  3. 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.
  4.  
  5. Example :
  6.  
  7. Input: head = [,,,-], pos =
  8. Output: true
  9. Explanation: There is a cycle in the linked list, where tail connects to the second node.
  10.  
  11. Example :
  12.  
  13. Input: head = [,], pos =
  14. Output: true
  15. Explanation: There is a cycle in the linked list, where tail connects to the first node.
  16.  
  17. Example :
  18.  
  19. Input: head = [], pos = -
  20. Output: false
  21. Explanation: There is no cycle in the linked list.
  22.  
  23. Follow up:
  24.  
  25. Can you solve it using O() (i.e. constant) memory?
  26.  

Ac Code: (Hash)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. bool hasCycle(ListNode *head) {
  12. unordered_set<ListNode*> visit;
  13. while(head)
  14. {
  15. if(visit.count(head)!=) return true;
  16. visit.insert(head);
  17. head = head->next;
  18. }
  19. return false;
  20. }
  21. };

Fast and Slow Pointer

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. bool hasCycle(ListNode *head) {
  12. ListNode *slow = head;
  13. ListNode *fast = head;
  14. while(fast){
  15. if(!fast->next) return false;
  16. fast = fast->next->next;
  17. slow = slow->next;
  18. if(slow == fast) return true;
  19. }
  20. return false;
  21. }
  22. };

leetcode 141 Linked List Cycle Hash fast and slow pointer的更多相关文章

  1. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  2. [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 ...

  3. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Python [Leetcode 141]Linked List Cycle

    题目描述: Given a linked list, determine if it has a cycle in it. 解题思路: 快的指针和慢的指针 代码如下: # Definition for ...

  9. 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 ...

随机推荐

  1. 对复杂字典Dictionary<T1,T2>排序问题

    原文:对复杂字典Dictionary<T1,T2>排序问题 //VoltageCount类(电压值对应的数量):    public class VoltageCount    {     ...

  2. mvn 命令在command prompt无法识别

    Download maven from this website: https://maven.apache.org/download.cgi 解压binary包后放到一个位置,比如C:\apache ...

  3. Android微信支付SDK

    App对接微信调起微信支付需要在微信平台注册,鉴别的标识就是App的包名,所以将申请的包名单独打包成一个Apk文件,则在其他的App调起此Apk的时候同样可以起到调用微信支付的功能.这样就实现了调起微 ...

  4. C++得到当前进程所占用的内存

    原文地址:C++得到当前进程所占用的内存作者:雪碧狗 使用SDK的PSAPI (Process Status Helper)中的BOOL GetProcessMemoryInfo(  HANDLE P ...

  5. C#获取带汉字的字符串长度

    正常情况下,我们是直接去string的length的,但是汉字是有两个字节的,所以直接用length是错的.如下图: 所以应该用以下代码来获取长度: private void button1_Clic ...

  6. 数据库连接池之_DButils

    // 这个是在添加数据 @Test public void demo1() { QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); ...

  7. 关于git远程分支操作

    对于用户来说,git给人提交到本地的机会.我们可以在自己的机器上创建不同的branch,来测试和存放不同的代码. 对于代码管理员而言,git有许多优良的特性.管理着不同的分支,同一套源代码可以出不一样 ...

  8. 中芯国际在CSTIC上悉数追赶国际先进水平的布局

    作为中国最大.工艺最先进的晶圆厂,中芯国际的一举一动都会受到大家的关注.在由SEMI主办的2017’中国国际半导体技术大会(CSTIC 2017)上,中芯国际CEO邱慈云博士给我们带来了最新的介绍. ...

  9. Java实现Qt的SIGNAL-SLOT机制(保存到Map中,从而将它们关联起来,收到信号进行解析,最后反射调用)

    SIGNAL-SLOT是Qt的一大特色,使用起来十分方便.在传统的AWT和Swing编程中,我们都是为要在 监听的对象上添加Listener监听器.被监听对象中保存有Listener的列表,当相关事件 ...

  10. ZooKeeper+Dubbo+SpringBoot 微服务Demo搭建

    1. 首先创建springBoot项目,springBoot是一堆组件的集合,在pom文件中对需要的组件进行配置.生成如下目录结构 创建test项目,同步在test创建dubbo-api,dubbo- ...