Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
思路:两条链表,从相交点开始之后的长度相同,但之前长度可能不同,所以走两遍就行。
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//boundary check
if(headA == null || headB == null) return null;
ListNode a = headA;
ListNode b = headB;
//if a & b have different len, then we will stop the loop after second iteration
while( a != b){
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
a = a == null? headB : a.next;
b = b == null? headA : b.next;
}
return a;
}