feat: 160_intersection_of_two_linked_lists
This commit is contained in:
parent
7a5e2cc77c
commit
5a8e1adf41
30
160_intersection_of_two_linked_lists/main.cpp
Normal file
30
160_intersection_of_two_linked_lists/main.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user