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

    相交链表

    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。

    示例 1:

    image

    输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
    输出:Intersected at '8'
    解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
    从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
    在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
    
    1
    2
    3
    4
    5

    # 解法:哈希集合

    var getIntersectionNode = function(headA, headB) {
        const visited = new Set();
        let temp = headA;
        while (temp !== null) {
            visited.add(temp);
            temp = temp.next;
        }
        temp = headB;
        while (temp !== null) {
            if (visited.has(temp)) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists

    ← 二叉树的锯齿形层序遍历 有效的括号→

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