Redian新闻
>
copy link with random additional pointers
avatar
copy link with random additional pointers# JobHunting - 待字闺中
c*2
1
复制linked list。 已知每个节点有两个pointer,一个指向后一个节点,另一个指向
其他任意一节点。 O(n)时间内,无附加内存,复制该linked list。(存储不连续)
avatar
s*e
2
I saw this problem earlier here. The answer is:
(imagine you have a->b->c->d, lets call the other pointer as "other"
1. duplicate each node and insert after itself. Now you get a->a->b->b->c->c->d->d
2. node n=head
while(n!=null)
n->next->other = n->other->next
n=n->next->next
3. separate the two lists
head2 = head->next
node n=head
while(n->next->next !=null)
n->next->next = n->next->next->next
n->next = n->next->next
n=n->next
n->next = null
n->next->next = null
不知道implement有没有错,不过idea就是这样
avatar
c*2
3
very good idea.
1) First pass: duplicate each node
2) Second pass: populate new other pointers
3) Third pass: split the list
Thanks!
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。