Copy List with Random Pointer

思路一:使用一个hashTable存储原List到新List的映射,其中tmpNode->random = head->random;记录random映射,不错这里tmpNode是新List中的结点,head是原List中的结点,经过两个映射(指向原List中的结点,映射至对应的新List)即可完成深拷贝。

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        RandomListNode dummy(-1);
        RandomListNode *cur = &dummy;
        
        unordered_map<RandomListNode *, RandomListNode *> hashTable;
        
        while(head != nullptr)
        {
            RandomListNode *tmpNode = new RandomListNode(head->label);
            cur->next = tmpNode;
            
            hashTable[head] = tmpNode;
            tmpNode->random = head->random;
            
            head = head->next;
            cur = cur->next;
        }
        
        cur = dummy.next;
        while(cur)
        {
            if(cur->random != nullptr)
                cur->random = hashTable[cur->random];
            cur = cur->next;
        }
        
        return dummy.next;
    }
};

思路二:上面的算法经过了两次遍历,实际经过一次遍历即可,这里面需要注意细节:当连接cur->next=tmpNode后,之后是为tmpNode寻找random

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        RandomListNode dummy(-1);
        RandomListNode *cur = &dummy;
        
        unordered_map<RandomListNode *, RandomListNode *> hashTable;
        while(head != nullptr)
        {
            RandomListNode *tmpNode = nullptr;
            if(hashTable.count(head) > 0)
                tmpNode = hashTable[head];
            else
            {
                tmpNode = new RandomListNode(head->label);
                hashTable[head] = tmpNode;
            }
                
            cur->next = tmpNode;
            
            if(head->random != nullptr)
            {
                if(hashTable.count(head->random) > 0)
                    tmpNode->random = hashTable[head->random];
                else
                {
                    tmpNode->random = new RandomListNode(head->random->label);
                    hashTable[head->random] = tmpNode->random; 
                }
            }
            
            cur = cur->next;
            head = head->next;
        }
        cur->next = nullptr;
        return dummy.next;
    }
};

思路三:也可是不使用hashTable实现

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        RandomListNode *cur = nullptr;
        
        for(cur = head; cur != nullptr;)
        {
            RandomListNode *tmpNode = new RandomListNode(cur->label);
            tmpNode->next = cur->next;
            cur->next = tmpNode;
            cur = tmpNode->next;
        }
        
        
        //for random dulplicate
        for(cur = head; cur != nullptr;)
        {
            if(cur->random != nullptr)
            {
                cur->next->random = cur->random->next;
            }
            cur = cur->next->next;
        }
        
        //split the list
        RandomListNode dummy(-1);
        RandomListNode *newCur = &dummy;
        for(cur=head; cur != nullptr;)
        {
            newCur->next = cur->next;
            newCur = newCur->next;
            cur->next = cur->next->next;
            cur = cur->next;
        }
        
        return dummy.next;
    }
};

 

posted @ 2017-03-20 09:52  chengcy  Views(125)  Comments(0)    收藏  举报