public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int counta = 0,countb =0;
ListNode preA = headA,preB=headB;
int length = 0;
while(headA != null){
counta++;
headA = headA.next;
}
while(headB != null){
countb++;
headB = headB.next;
}
if(countb-counta>0){
while(countb-counta-length!=0){
length++;
preB = preB.next;
}
}else{
while(counta-countb-length >0){
length++;
preA=preA.next;
}
}
while(preA != null && preB != null){
if(preA == preB){
return preA;
}else{
preA = preA.next;
preB = preB.next;
}
}
return null;
}
}