题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
class Solution {
public:
ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
unsigned int nlengthA = Getlength(headA);
unsigned int nlengthB = Getlength(headB);
unsigned int lengthDif = nlengthA - nlengthB;
ListNode* headLong = headA;
ListNode* headSh = headB;
if (nlengthA < nlengthB) {
lengthDif = nlengthB - nlengthA;
headLong = headB;
headSh = headA;
}
for (int i = 0; i < lengthDif; i++) headLong = headLong->next;
while (headLong != headSh){
headLong = headLong->next;
headSh = headSh->next;
}
ListNode *sameNode = headLong;
return sameNode;
}
unsigned int Getlength(ListNode *head){
int n=0;
ListNode* p = head;
while (p){
n++;
p = p->next;
}
return n;
}
};
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla