题目描述
blablabla
样例
blablabla
算法1
blablabla
时间复杂度分析:blablabla
java代码
class Solution {
public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
int alength=0;
ListNode node1=headA;
while (node1!=null){
node1=node1.next;
alength++;
}
int blength=0;
ListNode node2=headB;
while (node2!=null){
node2=node2.next;
blength++;
}
if (alength>blength){
int cha=alength-blength;
while (cha--!=0){
headA=headA.next;
}
while (headA!=headB){
headA=headA.next;
headB=headB.next;
}
return headA==headB?headA:null;
}
if (blength>alength){
int cha=blength-alength;
while (cha--!=0){
headB=headB.next;
}
while (headA!=headB){
headA=headA.next;
headB=headB.next;
}
return headA==headB?headA:null;
}
return null;
}
}