"138-copy list with random Pointer (copy of single-linked list with random pointers)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
A linked list is given such this each node contains an additional random pointer which could point to all node in the list or null.
Return a deep copy of the list.
Main Topic
A single-linked list contains a node pointer that points to any node or null, returning a deep copy of the linked list.
Thinking of solving problems
The first step: Copy the node, the copied node is placed in the node to be copied, still form a single linked list
Step two: Threaded random pointers
Step Three: Detach the original single linked list from the copy list
Code Implementation
Algorithm implementation class
Public class solution { PublicRandomlistnodecopyrandomlist(Randomlistnode head) {if(Head = =NULL) {return NULL; } copynode (head); Linkrandompointer (head);returnSplitlist (head); }/** * Copy the node, the copied node is placed on the node to be copied, still form a single linked list * * @param Head of the list * * * Public void Copynode(Randomlistnode head) {//Record the current AV to be copiedRandomlistnode node = head; while(Node! =NULL) {//Copy a new nodeRandomlistnode Copynode =NewRandomlistnode (Node.label);//Link the node to the node being copied and still form a single linked list .Copynode.next = Node.next; Node.next = Copynode; node = Copynode.next; } }/** * Threaded random pointer * * @param Head list */ Public void Linkrandompointer(Randomlistnode head) {//Record the current AV to be copiedRandomlistnode node = head; while(Node! =NULL) {//The random pointer has a point pointing to a specific node if(Node.random! =NULL) {random pointer to node copied nodeNode.next.random = Node.random.next; }//points to the next replicated nodenode = Node.next.next; } }/** * Split the list, restore the original linked list, and assemble the copy of the linked list * * @param Head link header * @return copy of the new linked list */ PublicRandomlistnodesplitlist(Randomlistnode head) {//New list headerRandomlistnode copyhead = Head.next;//The replicated node that is currently being processedRandomlistnode node = head;//node currently being copiedRandomlistnode copy; while(Node! =NULL){//points to the replication nodecopy = Node.next;//Node.next points to the next replicated nodeNode.next = Copy.next;//The next replicated node is not NULL if(Node.next! =NULL) {//Copy.next points to the next replicated nodeCopy.next = Node.next.next; }//Node points to the next replicated node to be processednode = Node.next; }returnCopyhead; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47745459"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "138-copy list with random Pointer (copy a single linked list with random pointers)"