-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetIntersectionNode.c
More file actions
43 lines (39 loc) · 1.34 KB
/
getIntersectionNode.c
File metadata and controls
43 lines (39 loc) · 1.34 KB
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
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//solution1: time complexity O(m*n)
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* current = headA;
ListNode* current2 = headB;
while (current) {
while (current2 != NULL && current2 != current) {
current2 = current2->next;
}
if (current2) {
cout << "Intersected at " << current2->val << endl;
return current2;
}
current2 = headB;
current = current->next;
}
return NULL;
}
};
//solution2: time complexity O(m + n)
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
if (!headA || !headB) return nullptr;
ListNode* p1 = headA;
ListNode* p2 = headB;
while (p1 != p2) { // 两个指针在相交点相遇,或者同时为 nullptr
p1 = p1 ? p1->next : headB; // 走完 headA,就跳到 headB
p2 = p2 ? p2->next : headA; // 走完 headB,就跳到 headA
}
return p1; // 这里要么是交点,要么是 nullptr(无交点)
}
* Note: It works because after one search, A and B come to the points of departure of the same length.