how to copy a linkedlist
来源:互联网 发布:网络兼职都是真的吗 编辑:程序博客网 时间:2024/05/17 04:57
Copy link-list, If You Can
Make a seprate copy of a linked list that has an extra pointer than theregular ones. This pointer points to any randam node in thelist.i.e.::node structure is---
struct node
{
type element
node *next;
node *other;
};
You are provided with head pointer
eg. *other pointer of first node can point to middle node and *other pointer of last node can point to frist node
I need not remind u to keep in mind the complexity..
struct node
{
type element
node *next;
node *other;
};
You are provided with head pointer
eg. *other pointer of first node can point to middle node and *other pointer of last node can point to frist node
I need not remind u to keep in mind the complexity..
Holy_Sin
Thursday, June 29, 2006
Thursday, June 29, 2006
Yes, can be done in O(n) time and O(1) space, not counting input and output lists...
Here is come C like code..
// Edge case/error detection missing.
LL * Copy (LL *head){
LL *tmp = head;
LL *prev = NULL;
LL *cur = NULL;
LL *savedNext = NULL;
// Interleave the new and old lists.
while (tmp){
cur = AllocNewLL();
savedNext = tmp->next;
tmp->next = cur;
cur->next = savedNext;
tmp = savedNext;
}
tmp = head;
// Get the correct other pointers for the new list.
while(tmp){
cur = tmp->next;
cur->other = tmp->other->next;
tmp = cur->next;
}
LL * copiedList = head->next;
// Restore the original list.
while (tmp){
cur = tmp->next;
tmp->next = cur->next;
tmp = cur->next;
}
return copiedList;
}
Basically, if original list was A1 -> A2 -> ... -> An
The we make a copy of node Ai. Call that node Bi.
We first make the list look like
A1 -> B1 -> A2 -> B2 -> ... -> An -> Bn (first while loop)
Nowto get the correct other pointer of Bi, we take the other pointer ofAi, and get the next. i.e say the other of Ai is pointing to Aj. Thenwhen we follow the next of Aj we get Bj which should be the other ofBi. (second while loop)
Now we restore the input list back to its form:
A1 -> A2 -> ...-> An. (third while loop)
Here is come C like code..
// Edge case/error detection missing.
LL * Copy (LL *head){
LL *tmp = head;
LL *prev = NULL;
LL *cur = NULL;
LL *savedNext = NULL;
// Interleave the new and old lists.
while (tmp){
cur = AllocNewLL();
savedNext = tmp->next;
tmp->next = cur;
cur->next = savedNext;
tmp = savedNext;
}
tmp = head;
// Get the correct other pointers for the new list.
while(tmp){
cur = tmp->next;
cur->other = tmp->other->next;
tmp = cur->next;
}
LL * copiedList = head->next;
// Restore the original list.
while (tmp){
cur = tmp->next;
tmp->next = cur->next;
tmp = cur->next;
}
return copiedList;
}
Basically, if original list was A1 -> A2 -> ... -> An
The we make a copy of node Ai. Call that node Bi.
We first make the list look like
A1 -> B1 -> A2 -> B2 -> ... -> An -> Bn (first while loop)
Nowto get the correct other pointer of Bi, we take the other pointer ofAi, and get the next. i.e say the other of Ai is pointing to Aj. Thenwhen we follow the next of Aj we get Bj which should be the other ofBi. (second while loop)
Now we restore the input list back to its form:
A1 -> A2 -> ...-> An. (third while loop)
- how to copy a linkedlist
- How to Copy a Website
- how to copy a virtualbox vdi
- HOW TO COPY A µVISION PROJECT
- A Vim Tip: How to copy content of work directory
- How to copy a datafile from ASM to a file system not using RMAN
- How to Move or Copy a Tablespace to Another Database【每日一译】--2012-11-03
- How to copy a datafile from ASM to a file system not using RMAN (Doc ID 428893.1)
- How do you define a copy constructor or assignment operator for a class that contains a pointer to a (abstract) base class?
- How To Programmatically Copy an IMG Element to the Clipboard
- Gnome-terminal, How to Copy selection to clipboard automatically?
- How To Programmatically Copy an IMG Element to the Clipboard
- How to write a Makefile
- How to write a Makefile
- how to write a makefile
- how to become a hacker
- How to Become a Hacker
- How to Read a Paper
- Peercast简介、分析及常见问题处理
- 蒙提霍尔问题
- oracle中设置自增主键简化
- GL ACCOUNT ON TAX DISTRIBUTION LINE SHOULD COME FROM ACCOUNT SETUP ON TAX CODE
- 我国七大军区
- how to copy a linkedlist
- 不要再问我关于Zebra打印程序的问题了
- 西风胡杨
- 环境编译错误
- C++箴言:接口继承和实现继承
- Servlet缓存
- 举例说下WinCE中Loadcepc 工具的使用方法
- Linux下svn命令
- C#[Serializable]在C#中的作用-NET 中的对象序列化