博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
138. Copy List with Random Pointer
阅读量:7199 次
发布时间:2019-06-29

本文共 1162 字,大约阅读时间需要 3 分钟。

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.

难度:medium

题目:给定一链表其每个结点包含一随机指针可以指向任意一结点或是为空。

思路:hash map

Runtime: 2 ms, faster than 72.52% of Java online submissions for Copy List with Random Pointer.

Memory Usage: 38.5 MB, less than 100.00% of Java online submissions for Copy List with Random Pointer.

/** * 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 {    public RandomListNode copyRandomList(RandomListNode head) {        Map
mrr = new HashMap<>(); RandomListNode ptr = head; while (ptr != null) { mrr.put(ptr, new RandomListNode(ptr.label)); ptr = ptr.next; } ptr = head; while (ptr != null) { RandomListNode node = mrr.get(ptr); node.next = mrr.get(ptr.next); node.random = mrr.get(ptr.random); ptr = ptr.next; } return mrr.get(head); }}

转载地址:http://kqzum.baihongyu.com/

你可能感兴趣的文章
分享Kali Linux 2017年第30周镜像文件
查看>>
Centos6.8部署jenkins2.46.2
查看>>
SQL Server 内存分配
查看>>
Java 之原型模式
查看>>
Spring+quartz实现动态化定时任务
查看>>
Linux之NTP时间服务器配置部署
查看>>
我的友情链接
查看>>
python 安装mssql扩展
查看>>
js实现点击copy,可兼容
查看>>
oracle安装笔记 win
查看>>
我的友情链接
查看>>
新建文章 1
查看>>
Web应用添加Struts 2 的支持(SSH框架研究)
查看>>
CrowdStrike:我们是如何发现Win64bit提权0day漏洞(CVE-2014-4113)
查看>>
众云推一站式营销让企业移动营销先人一步
查看>>
过度营销 微信未来将低于腾讯预期
查看>>
在PHP语言中使用JSON
查看>>
GrideView 网格控件
查看>>
系统管理员-Linux基础学习-第一部分内容。
查看>>
Django 入门学习(3)
查看>>