"Leetcode-Interview algorithm classic-java implementation" "138-copy list with random Pointer (copy a single linked list with random pointers)"

Source: Internet
Author: User

"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)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.