day37(12.18)——leetcode面试经典150

138. 随机链表的复制

138. 随机链表的复制

题目:

题解:

java 复制代码
/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        Map<Node,Node> map = new HashMap<>();
        while(cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}
相关推荐
历程里程碑6 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
YGGP7 小时前
【Golang】LeetCode 128. 最长连续序列
leetcode
你撅嘴真丑13 小时前
第九章-数字三角形
算法
uesowys13 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder13 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮13 小时前
AI 视觉连载1:像素
算法
智驱力人工智能14 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥14 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风14 小时前
代码随想录第十五天
数据结构·算法·leetcode