-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution52.java
More file actions
36 lines (31 loc) · 814 Bytes
/
Solution52.java
File metadata and controls
36 lines (31 loc) · 814 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
34
35
36
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lengthA = getLen(headA);
int lengthB = getLen(headB);
ListNode a = headA;
ListNode b = headB;
if (lengthA > lengthB) {
for (int i = 0; i < (lengthA - lengthB); i++) {
a = a.next;
}
} else {
for (int i = 0; i < (lengthB - lengthA); i++) {
b = b.next;
}
}
while (a != b) {
a = a.next;
b = b.next;
}
return a;
}
private int getLen(ListNode head) {
ListNode cur = head;
int count = 0;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
}