当前位置: 首页 > news >正文

【力扣hot100题】(024)环形链表

很简单的一题,两种思路都写出来了。

一种是哈希表,最容易想到的一种:

/**
 * 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*> st;
        while(head){
            if(st.find(head)!=st.end()) return 1;
            st.insert(head);
            head=head->next;
        }
        return 0;
    }
};

一种是快慢指针:

/**
 * 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) {
        if(head==nullptr) return 0;
        ListNode* slow=head;
        ListNode* fast=head;
        while(fast!=nullptr&&fast->next!=nullptr){
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast) return 1;
        }
        return 0;
    }
};

相关文章:

  • kali配置固定IP
  • AI 数字人短视频数字人口播源码:短视频内容生产的新引擎​
  • axios介绍以及配置
  • 【LeetCode】二叉树的递归遍历
  • promise使用及其方法
  • CMake在Windows环境下Visual Studio Code的使用
  • 利用deepseek直接调用其他文生图网站生成图片
  • [VolgaCTF 2025] Baby-Welcome,BrokeBroke,Field Rules
  • nginx的自定义日志
  • 爬虫:网络请求(通信)步骤,http和https协议
  • C++算法——分治
  • MVC编程
  • Web网页内嵌 Adobe Pdf Reader 谷歌Chrome在线预览编辑PDF文档
  • 程序化广告行业(46/89):竞价结算规则、底价策略与内部排名解析
  • WPF 自定义路由事件
  • 题解:蓝桥杯 2023 省 B 接龙数列 - dp + 哈希map
  • 高通Android 8.1/9/10/11/12/13 通过包名设置默认launcher
  • [MySql] 多表关系, 多表查询
  • 消息中心系统架构设计
  • 14 配置Hadoop集群-配置历史和日志服务
  • 牧童蝉网站建设/百度电脑版官方下载
  • 电子商务网站规划与设计/chatgpt网页
  • 核酸第三方检测机构/南宁seo手段
  • 南阳做网站电话/大连seo网站推广
  • web网站开发毕设/郑州seo技术
  • 施工企业的安全生产管理机构以及安全生产管理人员履行下列职责:( )/南京seo整站优化技术