forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
47 lines (34 loc) · 854 Bytes
/
main3.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
42
43
44
45
46
47
/// 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) {}
};
/// Two Pointers
/// Time Complexity: O(m + n)
/// Space Complexity: O(1)
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* pA = headA;
ListNode* pB = headB;
while(pA || pB){
if(pA == pB)
return pA;
if(pA) pA = pA->next;
else pA = headB;
if(pB) pB = pB->next;
else pB = headA;
}
return NULL;
}
};
int main() {
return 0;
}