# C++ who can tell me why my solution is wrong?

• ``````class Solution {
public:
set<ListNode*> setA,setB;
{
{
}
{
}
}
vector<ListNode*> result;
set_intersection(setA.begin(), setA.end(),
setB.begin(), setB.end(),
inserter(result,result.begin()));
if(result.size()==0)
return nullptr;
return result[0];
}
``````

};

• didn't debug into your code , so don't know what went wrong . It could be easier if you want to use set :

``````ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_set<ListNode*> s;
while(current)
{
s.insert(current);
current=current->next;
}
while(current)
{
if(s.count(current)) return current;
current = current->next;
}
return NULL;
}
``````

• @arctanx Check set_intersection if you really want to use it, you should write a customised compare function.

Actually there is totally no need to use high-level functions, check the following solution which is more like a `algorithm` instead of special built-in methods usage showing stuff.

``````class Solution {
public:
{
int lenA = 0, lenB = 0;
while(pA) pA = pA->next, lenA++;
while(pB) pB= pB->next, lenB++;
if(lenA > lenB) for(int i = 0; i < lenA-lenB; ++i) pA = pA->next;
else for(int i = 0; i < lenB-lenA; ++i) pB = pB->next;
while(pA)
{
if(pA == pB) return pA;
pA = pA->next;
pB = pB->next;
}
return NULL;
}
};
``````

• @LHearen Thank you

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.