141 . 环形链表

链接

https://leetcode.cn/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-interview-150

题面

思路 :

法1 :

用哈希表来存之前的遍历过的结点 ;

一遍遍历,在遍历的过程中,先判断是否当前结点在哈希表中出现过,如果出现过,直接返回true;

否则继续遍历,如果到遍历结束,证明没有环,直接返回false;

复制代码
class Solution {
public:
    bool hasCycle(ListNode *head) {
        set<ListNode*> st ;
        while(head){
            if(st.count(head)) return true;
            st.insert(head);
            head = head -> next ;
        }
        return false;
    }
};

法2

直接判断循环次数,因为也就最多也就1e4个结点,那么如果有环的话,那么一定会出现遍历次数大于10000的,在遍历的过程中,判断n是否大于10000,是的话,直接返回true;否则返回false ;

复制代码
class Solution {
public:
    bool hasCycle(ListNode *head) {
        int n = 0 ;
        while(head != nullptr){
            n++ ;
            head = head->next ;
            if(n>10010){
                return true;
            }
        } 
        return false;
    }
};

法3

快慢双指针 -- > 算是本题的最优解了 ;

定义一个快慢双指针,快的每次跑两步,慢的每次跑一步;

如果存在环的话,那么快慢双指针一定都会进入环中,用相对速度思考,慢的不懂,快的每次前进一步,那么在环中,两个一定会相遇 ;

复制代码
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(head == nullptr || head->next == nullptr) return false;
        ListNode* slow = head;
        ListNode* fast = head->next;
        while(slow != fast){
            if(fast == nullptr || fast->next == nullptr){
                return false;
            }
            slow = slow->next;
            fast = fast->next->next;
        } 
        return true;
    }
};
相关推荐
鱼跃鹰飞1 天前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
Queenie_Charlie1 天前
小陶的疑惑2
数据结构·c++·树状数组
Queenie_Charlie1 天前
小陶与杠铃片
数据结构·c++·树状数组
云深处@1 天前
【C++】AVL树
数据结构
Yvonne爱编码1 天前
JAVA数据结构 DAY4-ArrayList
java·开发语言·数据结构
czxyvX1 天前
016-二叉搜索树(C++实现)
开发语言·数据结构·c++
蒟蒻的贤1 天前
leetcode链表
算法·leetcode·链表
执着2591 天前
力扣hot100 - 94、二叉树的中序遍历
数据结构·算法·leetcode
-dzk-1 天前
【代码随想录】LC 707.设计链表
数据结构·c++·算法·链表
you-_ling2 天前
数据结构:3.栈和队列
数据结构