A deep copy of a linked list, the difference is that the list has an extra random pointer. Reference: http://blog.csdn.net/ljiabin/article/details/39054999
The procedure is very ingenious, divided into three steps, one is the new node, and placed after the old node, the second is to modify the new node of the random pointer, and the third is to break the old and new list.
Randomlistnode *randomlist (Randomlistnode *head) { //Copy each node and place the new node after the old node for(Randomlistnode *cur = head; Cur! =nullptr;) {Randomlistnode*node =NewRandomlistnode (cur->label); Node->next = cur->Next; Cur->next =node; Cur= node->Next; } //to modify the random pointer of a new node for(Randomlistnode *cur = head; Cur! =nullptr;) { if(Cur->random! = nullptr) Cur->next->random = cur->random->Next; Cur= cur->next->Next; } //break the old and new linked listRandomlistnode Dummy (-1); for(Randomlistnode *cur = head,*new_cur=&dummy; cur! =nullptr;) {New_cur->next = cur->Next; New_cur= new_cur->Next; Cur->next = cur->next->Next; Cur= cur->Next; } returnDummy.next; }View Code
Copy of Leetcode List with Random Pointer (23)