コード例 #1
0
 RandomListNode *copyRandomList(RandomListNode *head, unordered_map<RandomListNode *, RandomListNode*> &visitTable) {
     if (!head) return head;
     if (visitTable.count(head)) return visitTable[head];
     RandomListNode *dup = new RandomListNode(head->label);
     visitTable[head] = dup;
     dup->next = copyRandomList(head->next, visitTable);
     dup->random = copyRandomList(head->random, visitTable);
     return dup;
 }
コード例 #2
0
 RandomListNode *copyRandomList(RandomListNode *head) {
     if(!head) return NULL;
     if(visited.count(head)>0) return visited[head];
     RandomListNode* node = new RandomListNode(head->label);
     visited[head] = node;
     node->next = copyRandomList(head->next);
     node->random = copyRandomList(head->random);
     return node;
 }
 RandomListNode *copyRandomList(RandomListNode *head) {
     if( !head ) return NULL;
     
     if( Map.count( head ) ) return Map[ head ];
     
     RandomListNode *now = new RandomListNode( head->label );
     
     Map[ head ] = now;
     
     now->next = copyRandomList( head->next );
     now->random = copyRandomList( head->random );
     
     return now;
 }
コード例 #4
0
ファイル: copyRandomList.cpp プロジェクト: wythern/leetcode
	RandomListNode *copyRandomList(RandomListNode *head) {
		if (NULL == head)
			return NULL;


		if (mapRandom.find(head) != mapRandom.end()){
			return mapRandom[head];
		}

		RandomListNode* pNode = new RandomListNode(head->label);
		mapRandom[head] = pNode;
		pNode->next = copyRandomList(head->next);
		pNode->random = copyRandomList(head->random);
		return pNode;
	}
コード例 #5
0
 void test() {
     RandomListNode *head = new RandomListNode(-1);
     RandomListNode *node = new RandomListNode(1);
     head->next = node;
     head->random = node;
     node->random = node;
     RandomListNode *result = copyRandomList(head);
 }
コード例 #6
0
int main(int argc, char const *argv[])
{
	RandomListNode * head = new RandomListNode(1);
	head->next = new RandomListNode(2);
	head->next->next = new RandomListNode(4);
	head->random = head->next;
	head->next->next->random = new RandomListNode(5);


	RandomListNode * res = copyRandomList(head);

	return 0;
}
コード例 #7
0
		void test() {
			RandomListNode *p1= new RandomListNode(1);
			RandomListNode *p2= new RandomListNode(2);
			RandomListNode *p3= new RandomListNode(3);
			RandomListNode *p4= new RandomListNode(4);
			RandomListNode *p5= new RandomListNode(5);

			p1->next = p2;
			p2->next = p3;
			p3->next = p4;
			p4->next = p5;

			p1->random = p3;
			p2->random = p1;
//			p3->random = p5;
			p4->random = p3;
			p5->random = p2;
			printRandomList(copyRandomList(p1));
		}
コード例 #8
0
 RandomListNode * copyRandomList(RandomListNode * pstHead, unordered_map<RandomListNode *, RandomListNode *> & hmapSrcDst)
 {
     RandomListNode * pstLead = new RandomListNode(INT_MIN);
     
     for (RandomListNode * pstSrc = pstHead, * pstDst = pstLead; pstSrc != NULL; pstSrc = pstSrc -> next)
     {
         unordered_map<RandomListNode *, RandomListNode *>::iterator hmapIterator = hmapSrcDst.find(pstSrc);
         
         if (hmapIterator != hmapSrcDst.end())
         {
             pstDst -> next = hmapIterator -> second;
             break;
         }
         
         pstDst -> next     = new RandomListNode(pstSrc -> label);
         pstDst             = pstDst -> next;
         hmapSrcDst[pstSrc] = pstDst;
         
         if (pstSrc -> random == NULL)
         {
             pstDst -> random = NULL;
         }
         else
         {
             hmapIterator = hmapSrcDst.find(pstSrc -> random);
             
             if (hmapIterator != hmapSrcDst.end())
             {
                 pstDst -> random = hmapIterator -> second;
             }
             else
             {
                 pstDst -> random = copyRandomList(pstSrc -> random, hmapSrcDst);
             }
         }
     }
     
     return pstLead -> next;
 }
コード例 #9
0
 RandomListNode *copyRandomList(RandomListNode *head) {
     unordered_map<RandomListNode*, RandomListNode*> visitTable;//
     return copyRandomList(head, visitTable);
 }
コード例 #10
0
 RandomListNode * copyRandomList(RandomListNode * head)
 {
     unordered_map<RandomListNode *, RandomListNode *> hmapSrcDst;
     return copyRandomList(head, hmapSrcDst);
 }