-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_160.java
More file actions
33 lines (30 loc) · 806 Bytes
/
Copy pathP_160.java
File metadata and controls
33 lines (30 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package leetcode.easy;
import utils.DataStructures.ListNode;
@SuppressWarnings("ReturnOfNull")
public class P_160 {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode iterA = headA;
ListNode iterB = headB;
int reset = 0;
while (reset < 2) {
iterA = iterA.next;
iterB = iterB.next;
if (iterA == null) {
iterA = headB;
reset++;
}
if (iterB == null) {
iterB = headA;
reset++;
}
}
while (iterA != iterB) {
iterA = iterA.next;
iterB = iterB.next;
}
return iterA;
}
}