public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pL = headA;
ListNode pS = headB;
int lenA = 0;
while(pL != null){
lenA++;
pL = pL.next;
}
int lenB = 0;
while(pS != null){
lenB++;
pS = pS.next;
}
pL = headA;
pS = headB;
int len = lenA - lenB;
if(len < 0){
//单链表B是长的
pL = headB;
pS = headA;
len = lenB - lenA;
}
//最长的单链表永远是pL,并且差值len是一个正数
for(int i = 0;i < len; i++){
pL = pL.next;
}
//pL和pS在同一个起跑线上
while(pL != null && pS != null && pL != pS){
pL = pL.next;
pS = pS.next;
}
if(pL == pS && pL != null && pS != null ){
return pL;
}
return null;
}