剑指offer:25、复杂链表的复制

题目描述

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)


/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(!pHead)return NULL;
        RandomListNode *currNode=pHead;
        while(currNode)
        {
            RandomListNode* node =new RandomListNode(currNode->label);
            node->next=currNode->next;
            currNode->next=node;
            currNode=node->next;
                
        }
        currNode=pHead;
        
        while(currNode)
        {
            RandomListNode* node=currNode->next;
            if(currNode->random)
            {
                node->random=currNode->random->next;
            }
            currNode = node->next;
            
        }
        RandomListNode *pCloneHead = pHead->next;
        RandomListNode *temp;
        currNode=pHead;
        while(currNode->next)
        {
            temp=currNode->next;
            currNode->next=temp->next;
            currNode=temp;
            
        }
        return pCloneHead;
        
        
    }
};

猜你喜欢

转载自blog.csdn.net/qq_31442743/article/details/81606161