/**
* 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) {
//让p,q为两链表的头结点,一直走到尾部,之后重新回到对方头部,当他们两个相遇是恰好是公共节点
//时间复杂度On
auto p = headA,q = headB;
while(p != q){
if(p) p = p->next;
else p = headB;
if(q) q = q->next;
else q = headA;
}
return q;
}
};