LintCode-剑指Offer-(105)复制带随机指针的链表

class Solution {
public:
    /**
     * @param head: The head of linked list with a random pointer.
     * @return: A new head of a deep copy of the list.
     */
    //递归真是个好东西,没想到真能用一个变量实现,简洁明了啊,非常好。
    RandomListNode *copyRandomList(RandomListNode *head) {
        // write your code here
        RandomListNode* my;
        myfunc(head,my);
        return my;
    }
    void myfunc(RandomListNode* node,RandomListNode*& n){
        if(node==NULL)
        {
            n=NULL;
            return ;
        }
        n=new RandomListNode(node->label);
        n->random=node->random;
        myfunc(node->next,n->next);
    }
};

你可能感兴趣的:(LintCode)