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的更多相关文章

  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. WPF 代码实现动画

    <Window x:Class="wpf180709.Window2"        xmlns="http://schemas.microsoft.com/win ...

  2. jquery 包裹标签

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. 3D-Touch Home Screen Quick Actions 使用

    1. 3D-Touch简单介绍 3D-Touch是iPhone 6s推出的一种可以让你与手机进行互动的全新方式.这一次,iPhone 能够感应你按压屏幕的力度.除了轻点.轻扫.双指开合这些熟悉的 Mu ...

  4. 深入解析Android关机

    下图详细阐释了Android的关机顺序. 第一步: 按住电源按钮半秒钟(500ms). 第二步: 之后,PhoneWindowManager.java 将捕获长按电源按钮这一事件并调用“interce ...

  5. CentOS搭建python开发环境

    装了个CentOS 5.5,想在上面搭个python的开发环境,可是还是遇到了很多问题,记录一下过程: 1.python升级 查看python版本 python -V Python 2.4.3 因为p ...

  6. Android零基础入门第48节:可折叠列表ExpandableListView

    原文:Android零基础入门第48节:可折叠列表ExpandableListView 上一期学习了AutoCompleteTextView和MultiAutoCompleteTextView,你已经 ...

  7. Android之Log封装

    blog原文地址:http://yuxingxin.com/2015/10/26/AndroidLog/ Github:https://github.com/fallblank/CodeEssay

  8. mysql 的用户权限

    查看MySQL的用户权限 show grants for "username"@'host'; 添加新用户 允许本地IP访问localhost:127.0.0.1 use mysq ...

  9. xe5 for android 地理定位GPS

    先上源码,在解释. implementation uses androidapi.jni.JavaTypes, androidapi.jni.Location, FMX.helpers.android ...

  10. CrashRpt_v.1.4.2_vs2008_also_ok

    1.windows多线程程序release版崩溃记录工具,便于该如何查找错误. 2.此工具主要用来配置windbug工具,一种排查程序发布版本崩溃这种非常难处理的缺陷的方法,非常棒,amazing! ...