/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/publicclassSolution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = new ListNode(0);
ListNode B = new ListNode(0);
A = headA;
B = headB;
int lengthA = length(A);
int lengthB = length(B);
System.out.println(lengthA + "\t" + lengthB);
A = lengthA >= lengthB ? headA : headB;
B = lengthA >= lengthB ? headB : headA;
int n = Math.abs(lengthA - lengthB);
while (n > 0) {
A = A.next;
n -= 1;
}
if (A == B) return A;
while (A != null && B != null) {
if (A.next == B.next) return A.next;
A = A.next;
B = B.next;
}
returnnull;
}
publicintlength(ListNode l) {
int len = 0;
while (l != null) {
len += 1;
l = l.next;
}
return len;
}
}