Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 609 Bytes

52. 两个链表的第一个公共节点.md

File metadata and controls

30 lines (25 loc) · 609 Bytes

解题思路

双指针

A 从 a1 开始,走 L1+C 步,B 从 b1 开始,走 L2+C 步 A 遍历完后从 b1 开始,再走 L2 步,B 遍历完后从 a1 开始,再走 L2 步 到相遇点,A B 都走了 L1+L2+C 步

func getIntersectionNode(A, B *ListNode) *ListNode {
    curA, curB := A, B
    for curA != curB {
        if curA == nil {
            curA = B
        } else {
            curA = curA.Next
        }
        if curB == nil {
            curB = A
        } else {
            curB = curB.Next
        }
    }
    return curA
}