forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
41 lines (30 loc) · 809 Bytes
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/intersection-of-two-linked-lists/description/
/// Author : liuyubobobo
/// Time : 2018-10-01
#include <iostream>
#include <unordered_set>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Using HashSet
/// Time Complexity: O(m + n)
/// Space Complexity: O(m)
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_set<ListNode*> set;
for(ListNode* pA = headA; pA; pA = pA->next)
set.insert(pA);
for(ListNode* pB = headB; pB; pB = pB->next)
if(set.count(pB))
return pB;
return NULL;
}
};
int main() {
return 0;
}