Intersection of Two Linked Lists LeetCode solution in Java
1public class Solution {2public ListNode getIntersectionNode(ListNode headA, ListNode headB) {3ListNode tempA = headA, tempB = headB;4int lenA = 0, lenB = 0;5while (tempA != null) {6lenA++;7tempA = tempA.next;8}9while (tempB != null) {10lenB++;11tempB = tempB.next;12}13tempA = headA;14tempB = headB;15if (lenB > lenA) {16for (int i = 0; i < lenB - lenA; i++) {17tempB = tempB.next;18}19} else if (lenA > lenB) {20for (int i = 0; i < lenA - lenB; i++) {21tempA = tempA.next;22}23}24while (tempA != null && tempA != tempB) {25tempA = tempA.next;26tempB = tempB.next;27}28return tempA;29}30}