题目:160-相交链表
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// 任一为空,说明不相交
if (headA == nullptr || headB == nullptr) return nullptr;
ListNode *p = headA;
ListNode *q = headB;
// 题目保证了不存在环
while (p != q) {
if (p == nullptr) {
p = headB;
} else {
p = p->next;
}
if (q == nullptr) {
q = headA;
} else {
q = q->next;
}
}
return p;
}





Loading Comments...