public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode A = headA, B = headB;
// 当没有相交时循环
while (A != B) {
// 链表A到达末尾时,使链表A指向headB的头结点,否则前移
A = A == null ? headB : A.next;
// 链表B到达末尾时,使链表B指向headA的头结点,否则前移
B = B == null ? headA : B.next;
}
// 两个链表没有交点时,会一起到达终点,都为null
return A;
}
}
10-30
138
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-11
1073
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)