-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIntersectionOfTwoLinkedLists.cpp
73 lines (66 loc) · 1.33 KB
/
IntersectionOfTwoLinkedLists.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
72
73
/**
* 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) {
if (!headA || !headB) return nullptr;
int len1 = 0;
int len2 = 0;
ListNode* p1 = headA;
ListNode* p2 = headB;
while (p1) {
++len1;
p1 = p1->next;
}
while (p2) {
++len2;
p2 = p2->next;
}
p1 = headA;
p2 = headB;
for (int i = 0; i < len1 - len2; ++i)
p1 = p1->next;
for (int i = 0; i < len2 - len1; ++i)
p2 = p2->next;
while (p1 && p2 && p1 != p2) {
p1 = p1->next;
p2 = p2->next;
}
return p1 && p2 ? p1 : nullptr;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return nullptr;
ListNode* p1 = headA;
ListNode* p2 = headB;
bool p1Switched = false;
bool p2Switched = false;
while (p1 && p2 && p1 != p2) {
if (p1->next) {
p1 = p1->next;
} else if (!p1Switched) {
p1Switched = true;
p1 = headB;
} else {
p1 = nullptr;
}
if (p2->next) {
p2 = p2->next;
} else if (!p2Switched) {
p2Switched = true;
p2 = headA;
} else {
p2 = nullptr;
}
}
return p1 && p2 ? p1 : nullptr;
}
};