1. insert a duplicated node after each node in original list 2. copy the random ptr (make it point to the next node of original position) 3. break the list into 2 lists
【在 d**e 的大作中提到】 : 一个list的节点有 next 和 random 两个指针,如何copy这个list : 同学今天的ms面试题
how do you have it done if the original link list is readonly? limited space can be allocated.
【在 S******A 的大作中提到】 : 1. insert a duplicated node after each node in original list : 2. copy the random ptr (make it point to the next node of original : position) : 3. break the list into 2 lists
【在 S******A 的大作中提到】 : 1. insert a duplicated node after each node in original list : 2. copy the random ptr (make it point to the next node of original : position) : 3. break the list into 2 lists
S*A
9 楼
use an array to record the random index
space
【在 y****w 的大作中提到】 : how do you have it done if the original link list is readonly? limited space : can be allocated.
网上找到的答案: I would do this as a two-pass process. The first step would be to create the complete linked list using the next pointers. At the time you are doing tha t you'll want to construct a vector of pairs to pointers - the first pointer being the original node in A and the second pointer being its equivalent no de in B. The second pass of the algorithm will be to map the random pointers in B from the original A-node pointer to their equivalent B-node pointers u sing the map created in the firs
【在 d**e 的大作中提到】 : 一个list的节点有 next 和 random 两个指针,如何copy这个list : 同学今天的ms面试题
y*i
18 楼
有几点不是很明白
the tha pointer 第一次循环的时候并不知道vector应该开多大,总不能动态增加吧?那不是还需要一次 循环? 还有这个vector应该顺序存储pointer pairs么?那么第二次循环的时候寻找random pointer的时候难道要再遍历寻找这个指针在vector里的位置?除非用hash table存储 pointer pairs?那空间应该需求很大了 no pointers u secon
【在 c*********u 的大作中提到】 : 网上找到的答案: : I would do this as a two-pass process. The first step would be to create the : complete linked list using the next pointers. At the time you are doing tha : t you'll want to construct a vector of pairs to pointers - the first pointer : being the original node in A and the second pointer being its equivalent no : de in B. The second pass of the algorithm will be to map the random pointers : in B from the original A-node pointer to their equivalent B-node pointers u : sing the map created in the firs
s*t
19 楼
vector不用事先指定大小吧? 存的是指针,和链表顺序是一一对应的,直接 for (i = 0; i < n; i++){ *node->random = arr[i]; *node = *node->next; }
我修改过了。复制链表的random指针不用,而next指针指向原始链表的某个节点(由原 是链表的最初random指针决定) original:A -> B -> C -> D->... | | | | copy: A' B' C' D ... 假如B的最初random指针(现在指向B'了)指向D, 则B'的next指针指向D 换句话说,复制链表的next,用来保存原始链表中的最初(变为单向辐条前)的random 指针
This is the solution that the interviewer is looking for.
【在 S******A 的大作中提到】 : 1. insert a duplicated node after each node in original list : 2. copy the random ptr (make it point to the next node of original : position) : 3. break the list into 2 lists
O(n) 2 passes without touching original list, but needs O(n) space use a hashtable 1 pass: create a new list, random pointer points to original list. In the meantime build the hashtable 2 pass: iterate new list and update random pointer to point to the corresponding item in new list, with the help of hashtable
【在 d**e 的大作中提到】 : 一个list的节点有 next 和 random 两个指针,如何copy这个list : 同学今天的ms面试题