题目如下:
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.
分析如下:
使用hash,来建立第1个链表中的所有节点和第2个链表中的所有节点的一对一对应关系。
我的代码
/**
* 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:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head==NULL)
return NULL;
map<RandomListNode*, RandomListNode*> next1_next2_map;
RandomListNode* save_head=head;
RandomListNode* new_head=NULL;
RandomListNode* new_tail=NULL;
while(head!=NULL){
RandomListNode* cur=new RandomListNode(head->label);
next1_next2_map[head]=cur;
head=head->next;
}
head=save_head;
while(head!=NULL){
RandomListNode* cur=next1_next2_map[head];
cur->next=next1_next2_map[head->next];
cur->random=next1_next2_map[head->random];
if(new_head==NULL){
new_head=cur;
new_tail=cur;
}else{
new_tail->next=cur;
new_tail=new_tail->next;
}
head=head->next;
}
new_tail->next=NULL; //链表问题不要问了要用NULL结尾。
return new_head;
}
};
update 2014-12-21
按照上面的hash思路完成,简化为一个while循环搞定。可以优化时间复杂度的系数。
// solution 1 hashmap 440ms
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
map<RandomListNode*, RandomListNode*> old_to_new;
if (head == NULL) return NULL;
old_to_new[head] = new RandomListNode(head->label);
RandomListNode* head_new = old_to_new[head];
RandomListNode* previous_old = NULL;
RandomListNode* current_new = NULL;
old_to_new[NULL] = NULL;
while (head != NULL) {
if (old_to_new.find(head) == old_to_new.end())
old_to_new[head] = new RandomListNode(head->label);
current_new = old_to_new[head];
if (head->random != NULL && old_to_new.find(head->random) == old_to_new.end())
old_to_new[head->random] = new RandomListNode(head->random->label);
current_new->random = old_to_new[head->random];
if (previous_old != NULL)
old_to_new[previous_old]->next = current_new;
previous_old = head;
head = head->next;
}
return head_new;
}
};
可是其实还是不够快,网上搜到一个绝妙的办法。把每个新的node都插入原始链表中的对应的旧node的后面,这样非常方便就能找到新node在新list中的新random pointer,最后,拆分旧链表和新链表。
实际实现代码的时候注意,我一开始为了快,把找新random这一步和拆分新旧链表这一步放在一个循环里面完成,这样是可能会把旧链表写乱的,所以这两步是需要分开的。
//solution 2 copy each new node at the back of the corresponding old node
//一开始把step 2和step 3写在一起了,这样会造成original list被写乱,所以应该把这两步分开。
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head == NULL) return NULL;
RandomListNode* current = NULL;
RandomListNode* head_preserve = head;
// step1 create add new note into original list
while (head != NULL) {
current = new RandomListNode(head->label);
current->next = head->next;
head->next = current;
head = head->next->next;
}
//step 2 populate random pointer of new list
head = head_preserve;
while(head != NULL) {
current = head->next;
if (head->random != NULL)
current->random = head->random->next;
head = head->next->next;
}
// step3 seperate old list and new list
head = head_preserve;
head_preserve = head->next;
while(head != NULL) {
current = head->next;
head->next = head->next->next;
head = head->next;
if (head != NULL)
current->next = head->next;
}
return head_preserve;
}
};
参考阅读:
1 http://fisherlei.blogspot.com/2013/11/leetcode-copy-list-with-random-pointer.html