zoukankan      html  css  js  c++  java
  • 138. Copy List with Random Pointer (not do it by myself)

    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.

    Approach #1: Java.

    /**
     * Definition for singly-linked list with a random pointer.
     * class RandomListNode {
     *     int label;
     *     RandomListNode next, random;
     *     RandomListNode(int x) { this.label = x; }
     * };
     */
    public class Solution {
        HashMap<RandomListNode, RandomListNode> visitedHash = new HashMap<RandomListNode, RandomListNode>();
        
        public RandomListNode copyRandomList(RandomListNode head) {
            if (head == null)
                return null;
            
            if (this.visitedHash.containsKey(head))
                return this.visitedHash.get(head);
            
            
            RandomListNode node = new RandomListNode(head.label);
            
            this.visitedHash.put(head, node);
            
            node.next = this.copyRandomList(head.next);
            node.random = this.copyRandomList(head.random);
            
            return node;
        }
    }
    

      

    Approach #2: Python.

    # Definition for singly-linked list with a random pointer.
    # class RandomListNode(object):
    #     def __init__(self, x):
    #         self.label = x
    #         self.next = None
    #         self.random = None
    
    class Solution(object):
        def __init__(self):
            self.visited = {}
            
        def getClonedNode(self, node):
            if node:
                if node in self.visited:
                    return self.visited[node]
                else:
                    self.visited[node] = RandomListNode(node.label)
                    return self.visited[node]
            return None
            
        def copyRandomList(self, head):
            """
            :type head: RandomListNode
            :rtype: RandomListNode
            """
            if not head:
                return head
            
            old_node = head
            
            new_node = RandomListNode(old_node.label)
            self.visited[old_node] = new_node
            
            while old_node != None:
                new_node.random = self.getClonedNode(old_node.random)
                new_node.next = self.getClonedNode(old_node.next)
                
                old_node = old_node.next
                new_node = new_node.next
                
            return self.visited[head]
    
                
    

      

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

     At the first I can't understand the mean of this question, after seeing the Solution I understood. However, my basic of Link List is weak, so ^^^

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    VMware ESXI 5.5 注册码
    NetScaler Active-Active模式
    Citrix NetScaler HA(高可用性)解析
    服务管理--systemctl命令
    CentOS7 安装 webgoat 7.1 简介
    Codefoces 723B Text Document Analysis
    Codefoces 723A The New Year: Meeting Friends
    ECJTUACM16 Winter vacation training #1 题解&源码
    信息学奥赛一本通算法(C++版)基础算法:高精度计算
    从零开始学算法:高精度计算
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9960475.html
Copyright © 2011-2022 走看看