原题如下:
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];
}
};