-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path58_getIntersectionNode.java
46 lines (46 loc) · 1.09 KB
/
58_getIntersectionNode.java
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
public class Solution {
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Write your code here
if(headA==null||headB==null){
return null;
}
int lenA = 0;
int lenB = 0;
ListNode tmpNode = headA;
while(tmpNode.next!=null){
lenA++;
tmpNode = tmpNode.next;
}
tmpNode = headB;
while(tmpNode.next!=null){
lenB++;
tmpNode = tmpNode.next;
}
int tmp = lenA-lenB;
int dis = Math.abs(tmp);
ListNode node=null;
if(tmp<0){
tmpNode = headB;
node = headA;
}else{
tmpNode = headA;
node = headB;
}
while(tmpNode.next!=null){
if(dis--<=0){
if(tmpNode==node)
return node;
node = node.next;
}
tmpNode = tmpNode.next;
}
if(tmpNode==node)
return node;
return null;
}
}