【Lintcode】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.

题解:

Solution 1 ()

class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (head == nullptr) return nullptr;
        RandomListNode* dummy = new RandomListNode(-1);
        RandomListNode* cur = head;
        RandomListNode* node = dummy;
        unordered_map<RandomListNode*, RandomListNode*> map;
        while (cur) {
            RandomListNode* tmp = new RandomListNode(cur->label);
            node->next = tmp;
            map[cur] = node->next;
            node = node->next;
            cur = cur->next;
        }
        node = dummy->next;
        cur = head;
        while (node) {
            node->random = map[cur->random];
            node = node->next;
            cur = cur->next;
        }
        
        return dummy->next;
    }
};

Solution 2 ()

class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (!head) return head;
        RandomListNode* cur = head;
        while (cur) {
            RandomListNode* node = new RandomListNode(cur->label);
            node->next = cur->next;
            cur->next = node;
            cur = node->next;
        }
        cur = head;
        while (cur) {
            if (cur->random) {
                cur->next->random = cur->random->next;
            }
            cur = cur->next->next;
        }
        cur = head;
        RandomListNode* first = cur->next;
        while (cur) {
            RandomListNode* tmp = cur->next;
            cur->next = tmp->next;
            if (tmp->next) {
                tmp->next = tmp->next->next;
            }
            cur = cur->next;  
        }
        
        return first;
    }
};

Solution 3 ()

class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        RandomListNode *newHead, *l1, *l2;
        if (head == NULL) return NULL;

        for (l1 = head; l1 != NULL; l1 = l1->next) {
            l2 = new RandomListNode(l1->label);
            l2->next = l1->random;
            l1->random = l2;
        }
        
        newHead = head->random;
        for (l1 = head; l1 != NULL; l1 = l1->next) {
            l2 = l1->random;
            l2->random = l2->next ? l2->next->random : NULL;
        }
        
        for (l1 = head; l1 != NULL; l1 = l1->next) {
            l2 = l1->random;
            l1->random = l2->next;
            l2->next = l1->next ? l1->next->random : NULL;
        }

        return newHead;
    }
};

 

转载于:https://www.cnblogs.com/Atanisi/p/6849032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值