面试题 02.07. 链表相交 - 力扣(LeetCode)
A和B两个链表,A走完再走BD,B走完再走AD;
这样走的路程一样,动手画一下就发现D点他们会碰到
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* A = headA;
ListNode* B = headB;
while(A != B)
{
if(A == NULL)
{
A = headB;
}
else
A = A->next;
if(B == NULL)
{
B = headA;
}
else
B = B->next;
}
return A;
}
};