leetcode 138. 随机链表的复制 -- 哈希,回溯法,节点拆分

本文介绍了三种方法解决LeetCode中的随机链表复制问题:1.哈希法,两次遍历,分别构建新链表和设置random指针;2.优化的哈希法,一次遍历,利用哈希表存储旧链表节点;3.迭代和节点拆分法,先复制再调整random指针;4.递归方法,回溯并使用哈希表缓存。四种方法的时间复杂度均为O(n),空间复杂度分别为O(n)、O(n)、O(1)和O(n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

leetcode 138. 随机链表的复制

解法一:哈希

遍历两遍,第一遍创建节点,处理next,第二遍处理 random

/*
// 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) {
        Set<Node> oldLinkList = new HashSet<>();
        Map<Node, Node> newLinkList = new HashMap<>();
        Node dummyHead = new Node(-1), p = dummyHead, p1 = head;
        while (p1 != null) {
            Node newNode = new Node(p1.val);
            p.next = newNode;
            p = p.next;
            oldLinkList.add(p1);
            newLinkList.put(p1, newNode);
            if (p1.random == null) {
                newNode.random = null;
            }
            p1 = p1.next;
        }
        p.next = null;
        p = dummyHead.next;
        p1 = head;
        while (p1 != null && p != null) {
            if (p1.random != null && oldLinkList.contains(p1.random))
                p.random = newLinkList.get(p1.random);
            
            p = p.next;
            p1 = p1.next;
        }
        return dummyHead.next;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

解法一的优化:哈希

class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) return null;
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        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);
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

解法二:迭代 + 节点拆分

class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) return null;
        // 1 -> copy1 -> 2 -> copy2...
        for (Node node = head; node != null; node = node.next.next) {
            Node newNode = new Node(node.val);
            newNode.next = node.next;
            node.next = newNode;
        }
        for (Node node = head; node != null; node = node.next.next) {
            Node newNode = node.next;
            newNode.random = (node.random == null) ? null : node.random.next;
        }
        
        // 拆链
        Node newHead = head.next;
        for (Node node = head; node != null; node = node.next) {
            Node newNode = node.next;
            node.next = node.next.next;
            newNode.next = (newNode.next != null) ? newNode.next.next : null;
        }
        return newHead;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

解法三:递归(回溯+哈希表)

class Solution {
    Map<Node, Node> cachedNode = new HashMap<Node, Node>();

    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        if (!cachedNode.containsKey(head)) {
            Node headNew = new Node(head.val);
            cachedNode.put(head, headNew);
            headNew.next = copyRandomList(head.next);
            headNew.random = copyRandomList(head.random);
        }
        return cachedNode.get(head);
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/copy-list-with-random-pointer/solutions/889166/fu-zhi-dai-sui-ji-zhi-zhen-de-lian-biao-rblsf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值