-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (51 loc) · 1.03 KB
/
Copy pathSolution.java
File metadata and controls
52 lines (51 loc) · 1.03 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Solution
{
public ListNode getIntersectionNode(ListNode headA, ListNode headB)
{
int lenA = 0, lenB = 0;
ListNode a = headA;
while (a != null)
{
a = a.next;
lenA++;
}
ListNode b = headB;
while (b != null)
{
b = b.next;
lenB++;
}
int diff = Math.abs(lenA - lenB);
a = headA;
b = headB;
if (diff > 0)
{
if (lenA > lenB)
{
while (diff > 0)
{
a = a.next;
diff--;
}
}
else
{
while (diff > 0)
{
b = b.next;
diff--;
}
}
}
while (a != null)
{
if (a == b)
{
return a;
}
a = a.next;
b = b.next;
}
return null;
}
}