public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// headA和headB是否符合条件
if (headA == null || headB == null) {
return null;
}
// 维护两个具有差值的链表去遍历headA和headB
ListNode l1 = headA;
ListNode l2 = headB;
while (l1 != l2) {
l1 = (l1 == null) ? headB : l1.next;
l2 = (l2 == null) ? headA : l2.next;
}
return l1;
}
}
链表:1
最新推荐文章于 2021-09-26 12:06:31 发布