leetcode刷题思路总结_intersection two linked lists

在这里插入图片描述解题思路:
利用 STL的set集合 ——set集合元素集合唯一,不存在重复元素。
1.将A链表元素地址依次存入自定义set集合。
2.遍历B链表与set集合进行匹配,放回第一个匹配到的结点
`class Solution {
public:
std::set<ListNode*> myset;
ListNode* FindFirstCommonNode( ListNode* headA, ListNode* headB) {

    while(headA)
    {
        myset.insert(headA);
        headA=headA->next;
    }
    
    while(headB)
    {
        
        if(myset.find(headB)!=myset.end()) return headB;
        
        headB=headB->next;
        
    }
    
    return NULL;
}

};
`

猜你喜欢

转载自blog.csdn.net/ybxcsdn/article/details/87916704