From 5a8e1adf4147b4ff6565fef297a75156b60504a7 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Mon, 22 Sep 2025 10:58:43 +0800 Subject: [PATCH] feat: 160_intersection_of_two_linked_lists --- 160_intersection_of_two_linked_lists/main.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 160_intersection_of_two_linked_lists/main.cpp 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