Given a list of linked lists, each node of the list contains three attributes: 1, node value, 2, reference to the next node, 3, a reference to any node in the list, or not to any node. Copy the linked list

Source: Internet
Author: User
Idea: The difficulty of this algorithm is difficult to have a reference to a random node in the list, you can not determine which node the reference points to, but we can use the choice of reference to solve the problem. Using the original linked list as a reference, head is a node of the original list, and the next of this node is the one that is down, but now we can point this next to the copy linked list relative to the headclone of the original linked list. And then put Headclone next point to the head of next, and so on, and so on, to here already can see the law, this fusion of the original linked list and replication linked list of the new list has a feature, before and after two nodes for the original node and the relationship between the replication node, so that, The origin node of the random node refers to the node of the next node is a random node of the replication node refers to the node, and then the two linked list, the resulting one of the linked list is we need to copy the linked list, the code is as follows:
/* public class Randomlistnode {int label;
    Randomlistnode next = null;

    Randomlistnode random = null;
    Randomlistnode (int label) {this.label = label;
            }} */public class Solution {public Randomlistnode Clone (Randomlistnode phead) {if (phead==null) {
        return null;
        } Randomlistnode Tempnode;     
        Randomlistnode Pheadclone;
        Randomlistnode Newheadclone;        Randomlistnode newhead = new Randomlistnode (Phead.label);
            The head node of the new list for (Pheadclone=phead, newheadclone=newhead;pheadclone!=null;) {tempnode = Pheadclone.next;
            Pheadclone.next = Newheadclone;
            
            Newheadclone.next = Tempnode;         Preparation before entering the next cycle pheadclone = Tempnode;   Move the current node to the next node if (pheadclone!=null) {newheadclone = new Randomlistnode (Pheadclone.label); Copy current Node}}//copy random reference for(Pheadclone=phead, Newheadclone=newhead;pheadclone!=null;) {tempnode = Newheadclone.next;
            if (pheadclone.random!=null) {newheadclone.random = PHeadClone.random.next;
            }else{newheadclone.random = null;          
            } pheadclone = Tempnode;
            if (pheadclone!=null) {newheadclone = Pheadclone.next; }}//Split two linked lists for (Pheadclone=phead, newheadclone=newhead;pheadclone!=null;) {p   Headclone.next = Newheadclone.next;
            Restore the original linked list if (pheadclone.next!=null) {newheadclone.next = PHeadClone.next.next;
            }else {newheadclone.next = null;
            }//Prepare Next loop, reference change pheadclone = Pheadclone.next;
        Newheadclone = Newheadclone.next;
    } return newhead; }
}

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.