-
Notifications
You must be signed in to change notification settings - Fork 5
/
Solution.cpp
67 lines (62 loc) · 1.61 KB
/
Solution.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
#include <assert.h>
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
/* two pointers */
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) { return NULL; }
ListNode * p1 = headA, * p2 = headB;
bool switched1 = false, switched2 = false;
while (true) {
if (p1 == p2) {
return p1;
}
p1 = p1->next;
if (p1 == NULL) {
switch (switched1) {
case true:
return NULL;
default:
switched1 = true;
p1 = headB;
}
}
p2 = p2->next;
if (p2 == NULL) {
switch (switched2) {
case true:
return NULL;
default:
switched2 = true;
p2 = headA;
}
}
}
}
};
int main(int argc, char const *argv[])
{
ListNode n1 = ListNode(1);
ListNode n3 = ListNode(3);
ListNode n5 = ListNode(5);
ListNode n7 = ListNode(7);
ListNode n9 = ListNode(9);
ListNode n11 = ListNode(11);
ListNode n2 = ListNode(2);
ListNode n4 = ListNode(4);
n1.next = &n3;
n3.next = &n5;
n5.next = &n7;
n7.next = &n9;
n9.next = &n11;
n2.next = &n4;
n4.next = &n9;
Solution a;
assert(a.getIntersectionNode(&n1, &n2) == &n9);
return 0;
}