hgq's docs
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)

guoguoqiqi

漫不经心的向往
主页
ES6-阮一峰 (opens new window)
Vue文档 (opens new window)
Axios文档 (opens new window)
Vue Router (opens new window)
Vuex文档 (opens new window)
面试题-Vue (opens new window)
面试题-JS (opens new window)
  • 反转链表
  • 无重复字符的最长子串
  • LRU 缓存
  • 数组中的第K个最大元素
  • K个一组翻转链表
  • 三数之和
  • 手撕快速排序
  • 最大子数组和
  • 两数之和
  • 合并两个有序链表
  • 环形链表
    • 二叉树的层序遍历
    • 买卖股票的最佳时机
    • 二叉树的锯齿形层序遍历
    • 相交链表
    • 有效的括号
    • 二叉树的最近公共祖先
    • 合并两个有序数组
    • 搜索旋转排序数组
    • 最长回文子串
    • 岛屿的数量
    • 全排列
    • 字符串相加
    • 高频算法题
    guoguoqiqi
    2022-03-17

    环形链表

    给你一个链表的头节点 head ,判断链表中是否有环。

    如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

    如果链表中存在环 ,则返回 true 。 否则,返回 false 。

    示例 1:

    image

    输入:head = [3,2,0,-4], pos = 1
    输出:true
    解释:链表中有一个环,其尾部连接到第二个节点。
    
    1
    2
    3

    # 解法1:快慢指针

    var hasCycle = function(head) {
        if (head === null || head.next === null) return false
    
        let slow = head
        let fast = head.next
    
        while (fast !== slow) {
          if (fast === null || fast.next === null) return false
    
          slow = slow.next
          fast = fast.next.next
    
        }
    
        return true
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    # 解法2:哈希

    var hasCycle = function(head) {
        let map = new Map()
        let cur = head
        let res = false
    
        while (cur && cur.next && !res) {
          if (map.has(cur)) {
            res = true
          } else {
            map.set(cur, 0)
            cur = cur.next
          }
        }
    
        return res
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/linked-list-cycle

    ← 合并两个有序链表 二叉树的层序遍历→

    最近更新
    01
    vuex数据持久化怎么做
    05-22
    02
    vue的动态路由怎么配置使用
    05-22
    03
    vue权限控制一般怎么做
    05-22
    更多文章>
    Theme by Vdoing | Copyright © 2022-2022 Guoquoqiqi | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式