Copy List with Random Pointer

本文介绍了一种特殊链表的深拷贝方法,该链表节点包含额外的随机指针。通过三种不同思路及其实现代码,探讨如何高效地完成链表的复制,并确保新链表与原链表完全独立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

___________________________________________________________________________________________________________________


基本思路:  详细分析过程见 这里 ,提供了三种思路。

按照思路 1 实现,时间复杂度0(n^2),超时。

思路 2  实现代码如下:

RandomListNode *copyRandomList(RandomListNode *head) {
        
        if(NULL==head){
            return head;
        }
        
        //复制原始链表上的每个链表  并用 next 链接起来
        RandomListNode* Nhead = new RandomListNode(head->label);
        RandomListNode* iter = head;
        RandomListNode* Niter = Nhead;
        map<RandomListNode*,RandomListNode*> NodeMap;
        NodeMap[head] = Nhead;
        
        while(iter->next != NULL){
            Niter->next = new RandomListNode(iter->next->label);
            iter = iter->next;
            Niter = Niter->next;
            
            NodeMap[iter] = Niter;
        }
        
        //定位每个结点的m_pSibling
        iter = head;
        Niter = Nhead;
        while(iter != NULL){
            RandomListNode* TempRand = iter->random;
            if(TempRand==NULL){
                Niter->random = NULL;
            }
            else{
                
                Niter->random = NodeMap[iter->random];
                
            }
            
            iter = iter->next;
            Niter = Niter->next;
        }
        
        return Nhead;
    }


思路三实现参照了这里的代码 。实现过程中有 2 个地方(标注在代码中)需要注意,代码如下:

 void cloneNodes(RandomListNode* head){
        if(NULL==head){
            return;
        }
        
        RandomListNode* iter = head;
        while(iter != NULL){
            RandomListNode* node = new RandomListNode(iter->label);
            
            node->next = iter->next; 
            iter->next = node;
            iter = node->next;
        }
    }
    
    void connectRandomNodes(RandomListNode* head){
        if(NULL==head){
            return;
        }
        
        RandomListNode* iter = head;
        RandomListNode* copyIter = head->next;
        
        while(iter != NULL){
            if(iter->random != NULL){
                copyIter->random = iter->random->next;
            }else{
                copyIter->random = NULL;
            }
            
            iter = copyIter->next;
            
            if(iter != NULL){   // important!!!
                copyIter = iter->next;
            }
        }
    }

    RandomListNode* reconnectNodes(RandomListNode* head){
        if(NULL==head){
            return head;
        }
        
        RandomListNode* iter = head;
        RandomListNode* copyIter = head->next;
        RandomListNode* Nhead = head->next;
        
        while(iter != NULL){
            iter->next = copyIter->next;
            iter = iter->next;
            
            if(iter != NULL){  // important!!!
                copyIter->next = iter->next;
                copyIter = iter->next;
            }
        }
        
        return Nhead;
    }
    
    
    RandomListNode *copyRandomList(RandomListNode *head) {
        cloneNodes(head);
        connectRandomNodes(head);
        return reconnectNodes(head);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值