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];
    }
};
`std::ranges::copy`是C++20中引的一个算法,用于将一个范围内的元素复制到另一个范围中。它的声明如下: ```cpp template <class InputRange, class OutputIterator> constexpr OutputIterator copy(InputRange&& rng, OutputIterator result); ``` 其中,`InputRange`是一个表示输入范围的类型,可以是数组、容器或者其他支持迭代器的类型。`OutputIterator`是一个表示输出范围的迭代器类型,用于指定复制的目标位置。 `std::ranges::copy`函数会将输入范围中的元素复制到输出范围中,并返回指向复制结束位置的迭代器。复制的范围是从输入范围的起始位置到结束位置(不包括结束位置)。 下面是一个使用`std::ranges::copy`的示例代码: ```cpp #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> source = {1, 2, 3, 4, 5}; std::vector<int> destination; std::ranges::copy(source, std::back_inserter(destination)); for (const auto& num : destination) { std::cout << num << " "; } std::cout << std::endl; return 0; } ``` 输出结果为:`1 2 3 4 5`。 在上面的示例中,我们使用`std::ranges::copy`将`source`中的元素复制到`destination`中。`std::back_inserter`是一个输出迭代器,用于在容器的末尾插入元素。通过使用`std::back_inserter(destination)`作为`std::ranges::copy`的第二个参数,我们可以将复制的元素插入到`destination`的末尾。 需要注意的是,`std::ranges::copy`是C++20中引入的新特性,如果你使用的是较早的C++标准,可能无法使用该函数。在旧版本的C++中,你可以使用`std::copy`算法来实现类似的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值