-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_0207.java
54 lines (50 loc) · 1.39 KB
/
leetcode_0207.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
47
48
49
50
51
52
53
54
package leetcode;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
public class leetcode_0207 {
public static void main(String[] args){
}
}
/*
思路:注意是指针相同节点,并不是val数值相同
首先我们可以想到遍历二重,但是时间复杂度太高
由于相同节点后面的共用一条链表,股可以及逆行长度匹配
*/
class lee_0207{
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA, curB = headB;
int lengthA = 0, lengthB = 0;
while(curA!=null)//获取ab的长度
{
lengthA++;
curA = curA.next;
}
while(curB!=null)
{
lengthB++;
curB = curB.next;
}
curA = headA;//寻找A,B的公告交点
curB = headB;
if(lengthA > lengthB){
int gap = lengthA-lengthB;
while(gap>0){
curA = curA.next;
gap--;
}
}
else{
int gap = lengthB - lengthA;
while(gap>0){
curB = curB.next;
gap--;
}
}
while(curA!=null){
if(curA == curB)
return curA;
curA = curA.next;
curB = curB.next;
}
return null;
}
}