LintCode 105: Copy List with Random Pointer

原题如下:
105. 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.

Challenge
Could you solve it with O(1) space?

我的解法是用map。
解法1:

/**
 * 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:
    /**
     * @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) {
        if (!head) return NULL;
        map<RandomListNode*, RandomListNode*> mp; //original node, copied node
        
        RandomListNode *head2 = new RandomListNode(head->label);
        RandomListNode *node = head;
        RandomListNode *node2 = head2;
        mp[node] = node2;
        
        while(node->next) {
            node2->next = new RandomListNode(node->next->label);
            node = node->next;
            node2 = node2->next;
            mp[node] = node2;    
        }
        
        node = head;
        node2 = head2;
        while(node) {
            node2->random = mp[node->random];
            node = node->next;
            node2 = node2->next;
        }
        
        return head2;
    }
};

二刷:

/**
 * 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:
    /**
     * @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) {
        if (!head) return NULL;
        unordered_map<RandomListNode *, RandomListNode *> mp;
        RandomListNode * dummyHead = new RandomListNode(0);
        RandomListNode * origNode = head, * copyNode = dummyHead;
        RandomListNode * tmpNode;
        
        while(origNode) {
            tmpNode = new RandomListNode(origNode->label);
            mp[origNode] = tmpNode;
            copyNode->next = tmpNode;
            copyNode = tmpNode;
            origNode = origNode->next;
        }

        origNode = head;
        copyNode = dummyHead->next;
        while(origNode) {
            if (origNode->random) copyNode->random = mp[origNode->random];
            origNode = origNode->next;
            copyNode = copyNode->next;
        }
        
        return dummyHead->next;
    }
};

解法3:O(1)的解法。下次再做。

/**
 * 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:
    /**
     * @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) {
        if (!head) return NULL;
        RandomListNode *origHead = head;
        while(head) {
            RandomListNode *node = new RandomListNode(head->label);
            node->next = head->next;
            head->next = node;
            head = node->next;
        }
        head = origHead;
        
        while(head) {
            if (head->random) head->next->random = head->random->next;
            head = head->next->next;
        }

        RandomListNode *newHead = origHead->next;
        head = origHead;
        while(head) {
            RandomListNode *node = head->next;
            head->next = node->next;
            if (node->next) node->next = node->next->next;
            head = head->next;
        }
        return newHead;
    }
};

代码同步在
https://github.com/luqian2017/Algorithm

四刷:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (!head) return NULL;
        map<Node *, Node *> mp;
        Node *newHead = new Node(head->val);
        Node *node = head, *newNode = newHead;
        mp[node] = newNode;
        while (node->next) {
            node = node->next;
            newNode->next = new Node(node->val);
            newNode = newNode->next;
            mp[node] = newNode;
        }
        node = head, newNode = newHead;
        while (node) {
            if (node->random) {
                mp[node]->random = mp[node->random];
            }
            node = node->next;
        }
        return newHead;
    }
};

五刷:

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (!head) return NULL;
        unordered_map<Node *, Node *> mp;
        Node *node = head;
        while (node) {
            mp[node] = new Node(node->val);
            node = node->next;
        }
        node = head;
        while (node) {
            if (node->next) {
                mp[node]->next = mp[node->next];
            }
            if (node->random) {
                mp[node]->random = mp[node->random];
            }
            node = node->next;
        }
        return mp[head];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值