标签:val 链表 str 没有 head com 有一个 cpp 出现
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/class Solution {
public:
ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
auto it1=pHead1, it2 = pHead2;
//没有人到达终点
while(it1 != nullptr && it2 != nullptr){
//如果等长,会出现相等的情况
if(it1 == it2){
return it1;
}
it1 = it1->next;
it2 = it2->next;
}
//由于不等长,一定有一个先到达终点
ListNode* longone;
ListNode* shortone;
ListNode* itcontinue;
if(it1 == nullptr){
shortone = pHead1;
longone = pHead2;
itcontinue = it2;
}else{
shortone = pHead2;
longone = pHead1;
itcontinue = it1;
}
auto itlong = longone;
while(itcontinue){
itcontinue = itcontinue->next;
itlong = itlong->next;
}
auto itshort = shortone;
while(itshort != itlong){
itshort = itshort->next;
itlong = itlong->next;
}
return itshort;
}
};
标签:val 链表 str 没有 head com 有一个 cpp 出现
原文地址:https://www.cnblogs.com/N3ptuner/p/14587895.html