copy list with random pointer
来源:互联网 发布:淘宝店铺如何做爆款 编辑:程序博客网 时间:2024/05/01 08:57
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.
解法:
算法很巧妙,不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:
同理分两步
1、构建新节点random指针:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->next
2、恢复原始链表以及构建新链表:例如old1->next = old1->next->next, new1->next = new1->next->next
该算法时间复杂度O(N),空间复杂度O(1)
0 0
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- python模拟登录新浪微博
- [COGS371]亲和数解题报告
- 真相(truth)最可怕的敌人不是谎言(lie),而是神话(myth)---《唐浩明点评曾国藩家书》书评
- IOS多线程编程一:概述
- 用我的爱成全你的碧海蓝天
- copy list with random pointer
- NYOJ---19擅长排列的小明
- 初识Git,如何利用Git将代码托管到GitHub上
- 关于MSXML更改xml文件,removeChild方法成功但是文件依旧没有改变mxl文件
- 常用MyEclipse快捷键
- String,StringBuffer与StringBuilder的区别
- android webview和ji交互分析
- GC学习笔记
- dp(uva590Always on the run)