add: 220606

This commit is contained in:
Eat-Swap 2022-06-07 00:05:12 +08:00
parent 8c21db1446
commit da2ddf4ec9
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
1 changed files with 31 additions and 0 deletions

31
cpp/2206/220606.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <unordered_set>
struct ListNode {
int val;
ListNode* next;
explicit ListNode(int x, ListNode* n = nullptr) : val(x), next(n) {}
~ListNode() {
delete this->next;
this->next = nullptr;
}
};
/**
* 160. Intersection of Two Linked Lists
* Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
*/
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
std::unordered_set<ListNode*> s;
for (auto* ptr = headA; ptr; ptr = ptr->next)
s.insert(ptr);
for (auto* ptr = headB; ptr; ptr = ptr->next)
if (s.count(ptr))
return ptr;
return nullptr;
}
};