ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
// 在这里补充代码
ListNode* p1 = headA;
ListNode* p2 = headB;
int k = 0;
while (p1 != nullptr && p2 != nullptr) {
p1 = p1->next;
p2 = p2->next;
}
if (p1 == nullptr) {
while (p2 != nullptr) {
p2 = p2->next;
k++;
}
p2 = headB;
p1 = headA;
while (k--)
p2 = p2->next;
while (p1 != nullptr) {
if (p1 == p2)
return p1;
p1 = p1->next;
p2 = p2->next;
}
return nullptr;
} else {
while (p1 != nullptr) {
p1 = p1->next;
k++;
}
p2 = headB;
p1 = headA;
while (k--)
p1 = p1->next;
while (p2 != nullptr) {
if (p1 == p2)
return p1;
p1 = p1->next;
p2 = p2->next;
}
return nullptr;
}
}