leetcode IntersectionofTwoLinkedLists
z
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import utils.ListNode;

public class IntersectionofTwoLinkedLists {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA;
ListNode b = headB;
while (a.next != null && b.next != null) {
a = a.next;
b = b.next;
if (a == b) return a;
if (a.next == null) a = headB;
if (b.next == null) b = headA;
}
return null;
}
}