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);
    }
}
相关推荐
小辉同志几秒前
74. 搜索二维矩阵
c++·leetcode·矩阵·二分查找
programhelp_3 分钟前
Snowflake OA 2026 面经|3道高频真题拆解 + 速通攻略
经验分享·算法·面试·职场和发展
圣保罗的大教堂4 分钟前
leetcode 3740. 三个相等元素之间的最小距离 I 简单
leetcode
Duang5 分钟前
AI 真能自己写出整个 Windows 系统吗?我做了一场无监督实验
算法·设计模式·架构
少许极端8 分钟前
算法奇妙屋(四十五)-CCPC备战之旅-1
java·开发语言·算法
无小道9 分钟前
算法——找规律
算法·规律
圣保罗的大教堂39 分钟前
leetcode 2069. 模拟行走机器人 II 中等
leetcode
地平线开发者1 小时前
目标检测的 Anchor-Free 和 NMS 到底是什么?
算法·自动驾驶
海寻山1 小时前
Java枚举(Enum):基础语法+高级用法+实战场景+面试避坑
java·开发语言·面试
北顾笙9801 小时前
day24-数据结构力扣
数据结构·算法·leetcode