# 2.分析

`````` ListNode pl = headA;

int lenA = 0;
int lenB = 0;
while (pl != null) {

lenA++;
pl = pl.next;
}
//pl==null;

while (ps != null) {

lenB++;
ps = ps.next;
}
//ps==null;
``````

``````int len = lenA - lenB;//差值步
if (len < 0) {

len = lenB - lenA;
}
``````

`````` //pl走差值len步
while (len != 0) {

pl = pl.next;
len--;
}
//同时走，直到相遇
while (pl != ps) {

pl = pl.next;
ps = ps.next;
}
return pl;
}
``````

# 3.完整代码

``````//判断链表相交

return null;
}

int lenA = 0;
int lenB = 0;
while (pl != null) {

lenA++;
pl = pl.next;
}
//pl==null;

while (ps != null) {

lenB++;
ps = ps.next;
}
//ps==null;

int len = lenA - lenB;//差值步
if (len < 0) {

len = lenB - lenA;
}
//1、pl永远指向最长的链表  ps永远指向最短的链表   2、求到了差值len步

//pl走差值len步
while (len != 0) {

pl = pl.next;
len--;
}
//同时走，直到相遇
while (pl != ps) {

pl = pl.next;
ps = ps.next;
}
return pl;
}
``````
``````测试：
``````
`````` public static void main(String[] args) {

try {

} catch (NullPointerException e) {

e.printStackTrace();
System.out.println("没有相交结点！");
}

}
``````

``````MyLinkedList myLinkedList = new MyLinkedList();

try {

System.out.println(ret.val);
}catch (NullPointerException e){

e.printStackTrace();
System.out.println("不存在相交结点！");
}

}
``````