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);
    }
}
相关推荐
GreenTea4 小时前
深度解读 Anthropic 多智能体报告:更强的模型 ≠ 更好的协调
前端·后端·算法
码匠许师傅4 小时前
【C++ 面试真题】聊聊 C++ 的移动语义与右值引用
java·c++·面试
fqq35 小时前
力扣刷题前置Java语法
算法·leetcode·职场和发展
Thecozzy7 小时前
大厂 AI Coding 面试大招
人工智能·面试·职场和发展
半夏微凉半夏殇7 小时前
x11与weston对比,优先选哪个
leetcode·均值算法·eclipse
2501_906565127 小时前
数学之美探究
算法
oier_Asad.Chen7 小时前
【洛谷题解/AcWing题解/OI学习笔记】洛谷P2868【USACO07DEC】Sightseeing Cows G(01分数规划求最大比率)
算法·图论·spfa·二分·负环
Dr.kangder8 小时前
嵌入式面试总结(十五)——异常处理
单片机·面试·职场和发展·系统架构·嵌入式
leisoo80979 小时前
财报数据怎么排雷本地化Python构建财务异常预警系统
人工智能·python·算法
程序员爱钓鱼10 小时前
Go switch 详解
后端·面试·go