方法一:递归

    unordered_map<Node*,Node*> dict;
Node* copyRandomList(Node* head) {
if (!head) return head;
if (dict.count(head)) return dict[head];
dict[head]=new Node(head->val, nullptr, nullptr);
dict[head]->next=copyRandomList(head->next);
dict[head]->random=copyRandomList(head->random);
return dict[head];
}

方法二:非递归

    Node* copyRandomList(Node* head)
{
if (!head) return head;
unordered_map<Node*,Node*> m;
Node *p=head;
while(p) // make a copy of nodes
{
m[p]=new Node(p->val,nullptr,nullptr);
p=p->next;
}
p=head;
while(p) // link everyone and fill the random field
{
m[p]->next=m[p->next];
m[p]->random=m[p->random];
p=p->next;
}
return m[head];
}

最新文章

  1. win7的svchost.exe占用内存过高如何解决
  2. Android--ColorMatrix改变图片颜色
  3. 兼容IE与firefox的css 线性渐变(linear-gradient)
  4. 人脸识别的ppt
  5. 空格&amp;amp;nbsp在不同浏览器中显示距离不一致问题解决方法
  6. 【POJ 1330 Nearest Common Ancestors】LCA问题 Tarjan算法
  7. SQL Server 后续去除功能汇总
  8. Ubuntu tty中文字符乱码
  9. 【Python】debug工具-pdb(转)
  10. Spring Boot2.1.3全局跨域
  11. Django_简单的数据库交互案例
  12. kali源更新
  13. [转]每天一个linux命令(44):top命令
  14. django 有关session内部函数做法
  15. Matlab绘图添加直角坐标轴
  16. 【MyBatis】解析MyBatis传入参数的问题
  17. Laravel基础
  18. 使用Java Servlet进行简单登录
  19. python itertools模块练习
  20. SUSE Linux忘记root密码的对策

热门文章

  1. phpmailer绑定邮箱
  2. Oracle常见的表连接的方法
  3. Linux学习笔记 -- 话说文件
  4. SQL2008R转SQL2005
  5. verilog 之语法学习
  6. java成神之——jaxb操作xml的基本使用
  7. linux中memset的正确用法
  8. PC 微信页面倒计时代码 safari不兼容date的问题
  9. Mac hook—DYLD_INSERT_LIBRARIES
  10. laravel中间键组