/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
auto p=headA,q=headB; //注意理解本题题意
while(p!=q) //当p不等于q时
{
if(p) p=p->next; //如果p不等于0,p指向下一个
else p=headB; //否则,如果p指向0,则p=headb
if(q) q=q->next;
else q=headA;
}
return p; //不管两链表是否相交,最后都会等于同一值
}
};