feat: 160_intersection_of_two_linked_lists

This commit is contained in:
SquidSpirit 2025-09-22 10:58:43 +08:00
parent 7a5e2cc77c
commit 5a8e1adf41

View File

@ -0,0 +1,30 @@
#include <bits/stdc++.h>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
std::unordered_set<ListNode *> visited_node;
ListNode *current = headA;
while (current) {
visited_node.insert(current);
current = current->next;
}
current = headB;
while (current) {
if (visited_node.find(current) != visited_node.end()) {
return current;
}
current = current->next;
}
return NULL;
}
};