-
Notifications
You must be signed in to change notification settings - Fork 0
/
160. Intersection of Two Linked Lists.cpp
71 lines (61 loc) · 2.17 KB
/
160. Intersection of Two Linked Lists.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Runtime: 52 ms, faster than 98.21% of C++ online submissions for Intersection of Two Linked Lists.
//Memory Usage: 19.1 MB, less than 14.04% of C++ online submissions for Intersection of Two Linked Lists.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
vector<ListNode*> lA, lB;
ListNode *curA = headA, *curB = headB;
while(curA){
lA.push_back(curA);
curA = curA->next;
}
while(curB){
lB.push_back(curB);
curB = curB->next;
}
int i = 0;
while(i < min(lA.size(), lB.size())){
if(lA[lA.size()-1-i] != lB[lB.size()-1-i]){
break;
}
i++;
}
if(i == 0) return NULL;
return lA[lA.size()-1-(i-1)];
}
};
//https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/49785/Java-solution-without-knowing-the-difference-in-len!
//Two pointers
//time: O(m+n), space: O(1)
//Runtime: 52 ms, faster than 98.21% of C++ online submissions for Intersection of Two Linked Lists.
//Memory Usage: 16.8 MB, less than 41.28% of C++ online submissions for Intersection of Two Linked Lists.
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL) return NULL;
ListNode *pA = headA, *pB = headB;
//if length of A and B are different, we will stop at 2nd iteration
while(pA != pB){
//if the first list ends, we reset the pointer to the head of another list
pA = (pA == NULL)?headB:pA->next;
pB = (pB == NULL)?headA:pB->next;
}
/**
given
A = [4,8,4,5],
B = [5,0,8,4,5]
in the 1st iteration pA go through 1 node less than pB,
in the 2nd iteration pA go through 1 node more than pB,
so they will meet at the intersection point
**/
return pA;
}
};