相交链表-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;
    }
};
相关推荐
m0_547486664 小时前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr9 小时前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_39 小时前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.10 小时前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.10 小时前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣10 小时前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.99213 小时前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
无敌贵点大王21 小时前
RTThread学习记录11——RT-Thread 设备模型吃透:UART/ADC/PWM/PIN 到底有什么区别?
c语言·stm32·学习·链表
淡海水21 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1011 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质