其他分享
首页 > 其他分享> > 相交链表的交点

相交链表的交点

作者:互联网

 

https://leetcode.cn/problems/intersection-of-two-linked-lists/solution/xiang-jiao-lian-biao-by-leetcode-solutio-a8jn/

 

 

 

 

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func getIntersectionNode(headA, headB *ListNode) *ListNode {
    if headA==nil||headB==nil{
        return nil
    }
    ha:=headA;hb:=headB
    for ha!=hb{
        if ha==nil{
            ha=headB
        }else{
            ha=ha.Next
        }

        if hb==nil{
            hb=headA
        }else{
            hb=hb.Next
        }
    }
    return ha
}

//用hash集合标记存在的链表
func getList(headA, headB *ListNode) *ListNode {
    if headA==nil||headB==nil{
        return nil
    }
    mp:=make(map[*ListNode]bool,0)
    for headA!=nil{
        mp[headA]=true
        headA=headA.Next
    }

    for headB!=nil{
        if mp[headB]{
            return headB
        }
        headB=headB.Next
    }
    return nil
}

 

标签:ListNode,nil,相交,链表,headB,headA,交点,hb,ha
来源: https://www.cnblogs.com/-citywall123/p/16434147.html