题解 | #第一只公共的牛#
第一只公共的牛
https://www.nowcoder.com/practice/915476a66e714b32b240c3ee9e7ad5fb
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param headA ListNode类 * @param headB ListNode类 * @param headC ListNode类 * @return ListNode类 */ ListNode* findIntersection(ListNode* headA, ListNode* headB, ListNode* headC) { // write code here for (auto p = headA; p != nullptr; p = p->next) { cnt[p->val]++; } for (auto p = headB; p != nullptr; p = p->next) { cnt[p->val]++; } for (auto p = headC; p != nullptr; p = p->next) { cnt[p->val]++; } for (auto p = headA; p != nullptr; p = p->next) { if (cnt[p->val] == 3) return p; } return nullptr; } map<int, int> cnt = map<int, int>(); };