先测长度再双指针
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
if(!pHead1||!pHead2)return nullptr;
int n=0,m=0;
ListNode *ta=pHead1,*tb=pHead2;
while(ta=ta->next)
++n;
while(tb=tb->next)
++m;
ta=pHead1,tb=pHead2;
if(n>m){
for(int i=0;i<n-m;++i){
ta=ta->next;
}
}
else{
for(int i=0;i<m-n;++i){
tb=tb->next;
}
}
while(ta!=tb){
ta=ta->next;
tb=tb->next;
}
return ta;
}



京公网安备 11010502036488号