链表相交
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//第一种方法
std::set<ListNode*> pset;
ListNode* pheadA=headA;
ListNode* pheadB=headB;
while(pheadA)
{
pset.insert(pheadA);
pheadA=pheadA->next;
}
while(pheadB)
{
if(pset.find(pheadB)!=pset.end())
{
return pheadB;
}
pheadB=pheadB->next;
}
return NULL;
}
};