版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412861
思路
两个指针,先计算长度,相遇的时候,两个链表的长度一致
code
代码语言:javascript复制func getIntersectionNode(headA, headB *ListNode) *ListNode {
a, b := headA, headB
la, lb := 0, 0
for a != nil {
a = a.Next
la
}
for b != nil {
b = b.Next
lb
}
a, b = headA, headB
for la < lb {
b = b.Next
lb--
}
for lb < la {
a = a.Next
la--
}
for a != b {
a = a.Next
b = b.Next
}
return a
}