diff --git a/160_intersection_of_two_linked_lists/main.cpp b/160_intersection_of_two_linked_lists/main.cpp new file mode 100644 index 0000000..6dd127f --- /dev/null +++ b/160_intersection_of_two_linked_lists/main.cpp @@ -0,0 +1,30 @@ +#include + +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 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; + } +}; \ No newline at end of file