Title: A Linked list is given such this each node contains an additional random pointer which could point to any node in the L ist or null.
Return a deep copy of the list.
Analysis:
The topic gives a special single-linked list. Each node in the list has more than one pointer field: Random. Randomly only to one node of the linked list.
The topic asks us to give a deep copy of this list.
First of all, what is deep copy??
Part 1: Comparison of three types of Object Copy (for example in Array)
1. Shallow copy
Copying only copies of pointers or references, which means that only one copy of the data exists.
Of course, when it comes to efficiency, the problem is that if the original data changes, the copied object is changed because there is only one data!!!
Note: In fact, for the sake of efficiency, in some cases, it is not necessary to use shallow copy when more data is needed.
2. Deep copy
Unlike shallow copy, deep copy has multiple copies of the data. Therefore, deep copy is also more time-consuming.
In C + +, you can define your own copy constructor for a class to implement deep copy.
In Python, the default copy of array is shallow copy, which can take the copy module's deep copy
3. Lazy Copy
This is a combination of the two strategies described above. Also known as Copy-on-write.
When you initially copy, use a more efficient shallow copy and use a counter counter to record the number of objects that are currently share data.
When the data needs to be modified, according to counter, using Deep-copy,
That is, when you need to deep-copy, you call the more time-consuming deep-copy.
Back to the subject: Deep copy of the list
For a normal single-linked list, a copy of deep copy is very easy. Just loop through the list once, and then copy the nodes in turn.
But the problem is that Node has more than one random pointer field.
For the random field:
If the original linked list: node I points to node J
New Linked list: node I also points to node J (new linked list node)
How can I find the node that each node of the new linked list points to in the random field?
Idea: Step 1: First point at the back of each node in the original list, copy a new node, the original list length becomes twice times
The random pointer points to the node that follows the node that the random pointer points to in the original linked list node
Step 2: Split the list into two lists
/** * Definition for singly-linked list with a random pointer.
* struct Randomlistnode {* int label;
* Randomlistnode *next, *random;
* Randomlistnode (int x): label (x), Next (null), random (null) {} *}; */class Solution {Public:randomlistnode *copyrandomlist (Randomlistnode *head) {Randomlistnode *tHead = Hea
D
Randomlistnode *next = NULL;
while (tHead) {next = thead->next;
Randomlistnode *node = new Randomlistnode (Thead->label);
Node->next = thead->next;
Node->random = thead->random;
Thead->next = node;
Thead= Next;
} THead = head;
while (THead) {if (thead->random) Thead->next->random = thead->random->next;
THead = thead->next->next;
} randomlistnode *rethead = NULL;
Randomlistnode *tret = NULL;
THead = head; RAndomlistnode *next2 = NULL;
while (THead) {if (Rethead = = NULL) {next2 = thead->next->next;
Rethead = thead->next;
Tret = Rethead;
Thead->next = next2;
THead = next2;
} else {next2 = thead->next->next;
Tret->next = thead->next;
Thead->next = next2;
THead = next2;
Tret = tret->next;
}} return rethead; }
};