赞
踩
思路:遍历一次,先创建好所有节点,将节点和原链表放入哈希表 ,然后再次遍历,将所有结点对应连接
class Solution { public: Node* copyRandomList(Node* head) { if(!head) return head; Node* cur=head; unordered_map<Node*,Node*> keep; while(cur) { Node* copy=new Node(cur->val); keep[cur]=copy; cur=cur->next; } cur=head; while(cur) { keep[cur]->next=keep[cur->next]; keep[cur]->random=keep[cur->random]; cur=cur->next; } return keep[head]; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。