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);
    }
}
相关推荐
d111111111d2 小时前
STM32编码电机闭环PID调节教程。
笔记·stm32·单片机·嵌入式硬件·学习·面试
超级种码2 小时前
All In AI——DSPy框架,让智能体开发像模型训练一样
大数据·人工智能·算法
LYFlied2 小时前
【每日算法】LeetCode 79. 单词搜索
前端·算法·leetcode·面试·职场和发展
Chrikk2 小时前
C++20 Concepts 在算子库开发中的应用:从 SFINAE 到类型约束
人工智能·算法·c++20
炽烈小老头2 小时前
【每天学习一点算法 2025/12/18】对称二叉树
学习·算法
User_芊芊君子2 小时前
【LeetCode经典题解】:二叉树转字符串递归解法的核心逻辑与代码解剖
算法·leetcode·职场和发展
晴殇i2 小时前
【拿来就用】Uniapp路由守卫终极方案:1个文件搞定全站权限控制,老板看了都点赞!
前端·javascript·面试
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——计算右侧小于当前元素的个数
算法·leetcode·排序算法·职业发展
鹿角片ljp2 小时前
力扣110.平衡二叉树-递归
数据结构·算法·leetcode