-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection-of-two-linked-lists.cc
109 lines (90 loc) · 1.88 KB
/
intersection-of-two-linked-lists.cc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// https://oj.leetcode.com/problems/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) {
#if 1
ListNode *p = headA, *q = headB;
if (p == NULL || q == NULL) return NULL;
while (p != NULL && q != NULL && p != q) {
p = p->next;
q = q->next;
// 如果没有交点,最后p,q都为NULL
//
// Any time they collide or reach end together without colliding
// then return any one of the pointers.
//
if (p == q) return p;
//
// If one of them reaches the end earlier then reuse it
// by moving it to the beginning of other list.
// Once both of them go through reassigning,
// they will be equidistant from the collision point.
//
if (p == NULL) p = headB;
if (q == NULL) q = headA;
}
return p;
#else
#if 1
if (!headA || !headB) return NULL;
ListNode *pa = headA, *pb = headB;
ListNode *a_tail = NULL, *b_tail = NULL;
while (pa != pb) {
if (pa->next) {
pa = pa->next;
} else {
a_tail = pa;
pa = headB;
}
if (pb->next) {
pb = pb->next;
} else {
b_tail = pb;
pb = headA;
}
if (a_tail && b_tail && a_tail != b_tail) {
return NULL;
}
}
return pa;
#else
if (headA == NULL || headB == NULL) {
return NULL;
}
int alen = 1, blen = 1;
ListNode *p = headA, *q = headB;
while (p->next) {
alen++;
p = p->next;
}
while (q->next) {
blen++;
q = q->next;
}
if (p != q) {
return NULL;
}
if (alen < blen) {
swap(headA, headB);
swap(alen, blen);
}
for (int i = 0; i < alen - blen; i++) {
headA = headA->next;
}
while (headA != headB) {
headA = headA->next;
headB = headB->next;
}
return headA;
#endif
#endif
}
};