hopeztm
BAN USERMy solution to the problem is similar to MaxBrenner, but I made the new node and the corresponding source node address in the map, and use it as a reference and update the previous pointer.
typedef Node<int> IntNode;
void duplicate_linkList(IntNode *pSourceList,IntNode* &pTargeList)
{
if(pSourceList == NULL)
return ;
map<IntNode*,IntNode*> Map;
pTargeList = new IntNode(pSourceList->data);
Map.insert(std::map<IntNode*,IntNode*>::value_type(NULL,NULL));
Map.insert(std::map<IntNode*,IntNode*>::value_type(pSourceList,pTargeList));
IntNode *soureItr = pSourceList->next;
IntNode *tarItr = pTargeList;
while(soureItr != NULL)
{
IntNode *next = new IntNode(soureItr->data);
tarItr->next= next;
Map.insert(std::map<IntNode*,IntNode*>::value_type(soureItr,tarItr));
tarItr = next;
}
tarItr->next = NULL;
soureItr = pSourceList;
tarItr = pTargeList;
while(soureItr != NULL)
{
IntNode *t = (Map.find(soureItr->pre))->second;
tarItr->pre = t;
tarItr = tarItr->next;
soureItr = soureItr->next;
}
}
I wrote a line of code like
- hopeztm February 15, 2012pB[i] & m == 0
but what I really mean is (pB[i] & m) == 0
then I got trouble