The summary of the algorithm summarizes the linked list containing the random pointer node

Source: Internet
Author: User

A special list node class is described below:

public class node{

public int value;

Public Node Next;

Public Node Rand;

public Node (int data) {

This.value = Data

}

}

The rand pointer is a new pointer in the node class, which may point to any node in the linked list, or it may point to null

The general solution is introduced first:

Using a hash table

First, the linked list is traversed from left to right, and the corresponding replica node is copied for each node

Key 1 value 1 '

 PackageTT;ImportJava.util.HashMap; Public classTest95 { Public classnode{ Public intvalue;  PublicNode Next;  PublicNode Rand;  PublicNode (intdata) {             This. value=data; }            }                     Publicnode CopyListWithRand1 (node head) {HashMap<node, node> map=NewHashmap<node, node>(); Node cur=Head;  while(cur! =NULL) {map.put (cur,NewNode (Cur.value)); Cur=Cur.next; } cur=Head;  while(cur!=NULL) {map.get (cur). Next=Map.get (Cur.next); Map.get (cur). Rand=Map.get (Cur.rand); Cur=Cur.next; }        returnMap.get (head); }        }

The algorithm summarizes a list of nodes that contain 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.