相交链表-list

160. 相交链表 - 力扣(LeetCode)

链表没有直接求几个的size

用哈希表把heada存进哈希,在用哈希表的count(b),这个计算b出现几次,没出现就是=0;

cpp 复制代码
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        unordered_set<ListNode*> cmp;
        ListNode *tmp = headA;
        while(tmp){
            cmp.insert(tmp);
            tmp = tmp->next;
        }
        tmp = headB;

        while(tmp){
            if(cmp.count(tmp)){
                return tmp;
            }
            tmp = tmp->next;
        }
        return NULL;
    }
};
相关推荐
fei_sun2 分钟前
【数据结构】2025年真题
数据结构
我在人间贩卖青春5 分钟前
线性表之队列
数据结构·队列
1024小神11 分钟前
swift中 列表、字典、集合、元祖 常用的方法
数据结构·算法·swift
Java水解26 分钟前
基于Rust实现爬取 GitHub Trending 热门仓库
数据结构·后端
Luna-player1 小时前
在前端中list.map的用法
前端·数据结构·list
历程里程碑1 小时前
C++ 10 模板进阶:参数特化与分离编译解析
c语言·开发语言·数据结构·c++·算法
Byron Loong1 小时前
【Python】字典(dict)、列表(list)、元组(tuple)
开发语言·python·list
晨曦夜月2 小时前
笔试强训day5
数据结构·算法
H_z___2 小时前
Hz的计数问题总结
数据结构·算法
练习时长一年2 小时前
LeetCode热题100(搜索插入位置)
数据结构·算法·leetcode