public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
int s1 = 0, s2 = 0;
while (a != null){
a = a.next;
s1 ++;
}
while (b != null) {
b = b.next;
s2 ++;
}
a = headA;
b = headB;
if (s2 > s1) {
//交换链表
int temp = s1;
s1 = s2;
s2 =temp;
ListNode tempNode = a;
a = b;
b = tempNode;
}
int gap = s1 - s2;
while (gap -- > 0) { //走到与b同一起点
a = a.next;
}
while (a != null) {
if (a == b) return a;
a = a.next;
b = b.next;
}
return null;
}
}