• Copy List with Random Pointer


    1. Title

    Copy List with Random Pointer

    2. Http address

    https://leetcode.com/problems/copy-list-with-random-pointer/

    3. The question

    A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

    Return a deep copy of the list.

     4. My code(AC)

     1 class RandomListNode {
     2     int label;
     3     RandomListNode next,random;
     4     public RandomListNode(int x)
     5     {
     6             this.label = x;
     7     }
     8 
     9 }
    10 
    11 //Accpeted
    12     public static RandomListNode copyRandomList(RandomListNode head)
    13     {
    14             Map<RandomListNode,RandomListNode> mapH = new HashMap<RandomListNode,RandomListNode>();
    15             RandomListNode re,    q,end;
    16             RandomListNode p = head;
    17             re = null;
    18             end = null;
    19             while( p != null )
    20             {
    21                 q = new RandomListNode(p.label);
    22                 if( end == null)
    23                 {
    24                     re = q;
    25                     end = q;
    26                 }else{
    27                     end.next = q;
    28                     end = q;
    29                 }
    30                 mapH.put(p,q);
    31                 p = p.next;
    32             }
    33 
    34             p = head;
    35             q = re;
    36             while( p != null && q != null) {
    37                 if( p.random != null )
    38                 {
    39                     q.random = mapH.get(p.random);
    40                 
    41                 }else{
    42                     q.random = null;
    43                 }
    44                 p = p.next;
    45                 q = q.next;
    46             }
    47         return re;
    48 
    49     }
     
  • 相关阅读:
    测试VPS
    [转] 如何在vps上安装和登录Xwindows
    [转]设置修改CentOS系统时区
    顺序队列
    求二叉树的高度
    VMware Workstation cannot connect to the virtual machine
    如何查看hadoop是32位还是64位
    64位CentOS上编译 Hadoop 2.2.0
    hadoop 2.X下eclipse配置
    删除文件及文件夹
  • 原文地址:https://www.cnblogs.com/ordili/p/4970002.html
Copyright © 2020-2023  润新知