力扣每日刷题

138. 随机链表的复制 - 力扣(LeetCode)

这道题主要的问题就是如何确定每个节点的随机指针指向的是哪个节点。只要解决了这个问题那么这道题就解决了。

写题思路(哈希):

不递归:先遍历一遍旧链表,使用哈希表记录每个旧节点和节点在这个链表的下标(和新建的链表对应上),根据旧链表建立新链表并把新链表的前后相连,使用数组按照链表节点在这个链表的位置进行记录。最后在遍历一遍旧链表,获得旧链表的随机节点在哈希表中对应的下标,最后根据下表去数组中取新链表的节点进行连接就好。

代码:

复制代码
public static Node copyRandomList(Node head) {
        if(head==null){
            return null;
        }
        HashMap<Node,Integer> hashMap=new HashMap<>();
        List<Node> list=new ArrayList<>();
        int i=0;
        Node pre=head.next;
        Node p=new Node(head.val);
        list.add(p);
        hashMap.put(head,i);
        i++;
        while(pre!=null){
            hashMap.put(pre,i);
            Node now=new Node(pre.val);
            list.add(now);
            p.next=now;
            p=p.next;
            pre=pre.next;
            i++;
        }
        p=list.getFirst();
        while(head!=null){
            if(head.random!=null) {
                int k = hashMap.get(head.random);
                p.random = list.get(k);
            }
            p=p.next;
            head=head.next;
        }
        return list.getFirst();
}

递归:使用哈希表记录每个旧链表节点和对应的新链表结点,对于每个节点都可以进行判断,如果这个节点不在哈希表中,我们就创建一个新链表节点,并把旧链表的next节点和random节点作为参数进行传入。最后返回创建好的新节点。

代码:

复制代码
public static Node copyRandomList(Node head,int i){
        if(head==null){
            return null;
        }
        if(!map.containsKey(head)){
            Node node=new Node(head.val);
            map.put(head,node);
            node.next=copyRandomList(head.next);
            node.random=copyRandomList(head.random);
        }
        return map.get(head);
}
相关推荐
Zach_yuan30 分钟前
算法1111
算法
不穿格子的程序员1 小时前
从零开始刷算法——二分-搜索旋转排序数组
数据结构·算法
做怪小疯子1 小时前
LeetCode 热题 100——哈希——最长连续序列
算法·leetcode·哈希算法
Dream it possible!2 小时前
LeetCode 面试经典 150_二叉树_二叉树展开为链表(74_114_C++_中等)
c++·leetcode·链表·面试·二叉树
做怪小疯子2 小时前
LeetCode 热题 100——双指针——三数之和
算法·leetcode·职场和发展
高山上有一只小老虎2 小时前
等差数列前n项的和
java·算法
sin_hielo2 小时前
leetcode 2536
数据结构·算法·leetcode
flashlight_hi2 小时前
LeetCode 分类刷题:203. 移除链表元素
算法·leetcode·链表
py有趣2 小时前
LeetCode算法学习之数组中的第K个最大元素
学习·算法·leetcode
吗~喽2 小时前
【LeetCode】将 x 减到 0 的最小操作数
算法·leetcode