剑指offer-25、复杂链表的复制

题⽬描述

输⼊⼀个复杂链表(每个节点中有节点值,以及两个指针,⼀个指向下⼀个节点,另⼀个特殊指针random 指向⼀个随机节点),请对此链表进⾏深拷⻉,并返回拷⻉后的头结点。(注意,输出结果中请不要返回参数中的节点引⽤,否则判题程序会直接返回空)

思路及解答

哈希表映射

使用哈希表存储原节点和新节点的映射关系:

  1. 第一次遍历:创建所有新节点,并建立原节点到新节点的映射
  2. 第二次遍历:根据映射关系设置新节点的nextrandom指针
java 复制代码
public class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        
        // 创建哈希表存储原节点到新节点的映射
        HashMap<Node, Node> map = new HashMap<>();
        Node current = head;
        
        // 第一次遍历:创建所有新节点并建立映射
        while (current != null) {
            map.put(current, new Node(current.val));
            current = current.next;
        }
        
        // 第二次遍历:设置新节点的next和random指针
        current = head;
        while (current != null) {
            Node newNode = map.get(current);
            newNode.next = map.get(current.next);
            newNode.random = map.get(current.random);
            current = current.next;
        }
        
        return map.get(head);
    }
}

class Node {
    int val;
    Node next;
    Node random;
    
    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
  • 时间复杂度:O(n),两次遍历链表
  • 空间复杂度:O(n),需要存储所有节点的映射关系

节点插入拆分法

通过在原链表中插入新节点来避免使用额外空间:

  1. 节点复制插入:在每个原节点后面插入一个复制的新节点
  2. 设置random指针:新节点的random指向原节点random的下一个节点
  3. 链表拆分:将混合链表拆分为原链表和新链表
java 复制代码
public class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        
        // 第一步:在每个节点后面插入复制的节点
        Node current = head;
        while (current != null) {
            Node newNode = new Node(current.val);
            newNode.next = current.next;
            current.next = newNode;
            current = newNode.next;
        }
        
        // 第二步:设置复制节点的random指针
        current = head;
        while (current != null) {
            if (current.random != null) {
                current.next.random = current.random.next;
            }
            current = current.next.next;
        }
        
        // 第三步:拆分链表
        Node newHead = head.next;
        current = head;
        while (current != null) {
            Node temp = current.next;
            current.next = temp.next;
            if (temp.next != null) {
                temp.next = temp.next.next;
            }
            current = current.next;
        }
        
        return newHead;
    }
}
  • 时间复杂度:O(n),三次遍历链表
  • 空间复杂度:O(1),只使用固定数量的指针变量
相关推荐
1104.北光c°4 分钟前
【黑马点评项目笔记 | 优惠券秒杀篇】构建高并发秒杀系统
java·开发语言·数据库·redis·笔记·spring·nosql
ruleslol6 分钟前
普通流(Stream<T>)和原始类型特化流(IntStream, LongStream, DoubleStream)的区别
java
隐退山林6 分钟前
JavaEE初阶:文件操作和IO
java·java-ee
2501_907136827 分钟前
PDF增效工具 Quite imposing plus6
java·开发语言
Jaxson Lin11 分钟前
Java编程进阶:智能仿真无人机项目3.0
java·笔记·无人机
是阿楷啊12 分钟前
Java求职面试实录:互联网大厂场景技术点解析
java·redis·websocket·spring·互联网·大厂面试·支付系统
_周游21 分钟前
Java8 API文档搜索引擎_3.搜索模块(实现细节)
java·搜索引擎·intellij-idea
人道领域25 分钟前
SSM从入门到入土(Spring Bean实例化与依赖注入全解析)
java·开发语言·spring boot·后端
long31629 分钟前
Z算法(线性时间模式搜索算法)
java·数据结构·spring boot·后端·算法·排序算法
毕设源码-赖学姐30 分钟前
【开题答辩全过程】以 基于Java web的宠物领养系统的设计与实现为例,包含答辩的问题和答案
java·开发语言·宠物