【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;
    }
};
时间: 2024-10-19 11:37:45

【Lintcode】105.Copy List with Random Pointer的相关文章

【LeetCode】138. 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. 提示: 此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第

【LeetCode】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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

LintCode - Copy List with Random Pointer

LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/copy-list-with-random-pointer/ Description A linked list is given such that each node con

LeetCode: Copy List with Random Pointer [138]

[题目] 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. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

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. /** * Definition for singly-linked list with a random pointer. * struct RandomListNode

[leetcode]Copy List with Random Pointer @ Python

原题地址:https://oj.leetcode.com/problems/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. 解题思路:这题主要是需要深

Copy List with Random Pointer leetcode java

题目: 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. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

[LeetCode][JavaScript]Copy List with Random Pointer

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. https://leetcode.com/problems/copy-list-with-random-poin