
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3begin to intersect at node c1.
Notes:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
// 计算链表A的长度
int lenA = 1;
ListNode p = headA;
while (p.next != null) {
lenA++;
p = p.next;
}
// 计算链表B的长度
int lenB = 1;
ListNode q = headB;
while (q.next != null) {
lenB++;
q = q.next;
}
// 若A和B的最后一个结点不等,则不相交,返回null
if (p != q) {
return null;
}
// 链表按照尾部对齐
if (lenA > lenB) {
int t = lenA - lenB;
while (t-- != 0) {
headA = headA.next;
}
} else {
int t = lenB - lenA;
while (t-- != 0) {
headB = headB.next;
}
}
// 同时向前走
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。