• 【牛客-剑指offer-数据结构篇】JZ35 复杂链表的复制 两种方案 Java实现



    1 题目链接

    https://www.nowcoder.com/exam/oj/ta?page=1&tpId=13&type=13

    2 题目

    在这里插入图片描述
    在这里插入图片描述

    3 思路 & 代码

    两个方案思路差不多,一个使用链表中的label做map的key,一个是用整个节点作链表的key

    方案一 HashMap map

    1. 先忽视链表的random域,按照label和next域创建一条新链表

    2. 在创建的同时,以label为键,node为值,将新创建的节点放入hashmap中

    3. 这时存入hashmap的node只有label这个属性有值,next和random都为null。不过,在创建下一个node时,上一个node的next就会被赋值。

    4. 再次遍历题目所给的链表,这次遍历是处理新链表的random域

    5. 依据链表的label值,以及节点random域指向节点的label值,在hashmap中查找与这两个label对应的两个节点,然后为新链表的random域赋值,赋值前需要判断原链表的random域是否为空

      • 如果random有指向的节点,则直接赋值即可

      -如果原链表的random域为null,那么新链表的random域也赋值为null(这步操作可以省略,因为在创建该节点时,next和random都已经赋值为null了)

    import java.util.*;
    /*
    public class RandomListNode {
        int label;
        RandomListNode next = null;
        RandomListNode random = null;
    
        RandomListNode(int label) {
            this.label = label;
        }
    }
    */
    public class Solution {
        public RandomListNode Clone(RandomListNode pHead) {
    
            if (pHead == null) {
                return null;
            }
    
            HashMap<Integer, RandomListNode> map = new HashMap<>();
            RandomListNode tmp = pHead;
    
    		//记录新链表的头节点
            RandomListNode head = new RandomListNode(-1);
    		
    		//创建新链表时,不断移动的两个指针
            RandomListNode pre = head;
            RandomListNode node = null;
            
            //处理next域,按照next域先把链表连起来
            while (tmp != null) {
                node = new RandomListNode(tmp.label);
                pre.next = node;
                pre = node;
                map.put(node.label, node);
                tmp = tmp.next;
            }
    
            //处理random域
            tmp = pHead;
            while (tmp != null) {
                node = map.get(tmp.label);
                if (tmp.random != null) {
                    node.random = map.get(tmp.random.label);
                }
                tmp = tmp.next;
            }
    
            return head.next;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    方案二 HashMap map

    import java.util.*;
    /*
    public class RandomListNode {
        int label;
        RandomListNode next = null;
        RandomListNode random = null;
    
        RandomListNode(int label) {
            this.label = label;
        }
    }
    */
    public class Solution {
        public RandomListNode Clone(RandomListNode pHead) {
    
            if (pHead == null) {
                return null;
            }
    
            HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
            RandomListNode tmp = pHead;
    
            RandomListNode head = new RandomListNode(-1);
            RandomListNode pre = head;
            RandomListNode node = null;
            //处理next域
            while (tmp != null) {
                node = new RandomListNode(tmp.label);
                pre.next = node;
                pre = node;
                map.put(tmp, node);
                tmp = tmp.next;
            }
    
            //处理random域
            tmp = pHead;
            while (tmp != null) {
                node = map.get(tmp);
                if (tmp.random != null) {
                    node.random = map.get(tmp.random);
                }
                tmp = tmp.next;
            }
    
            return head.next;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    Chapter3 Pytorch与机器学习有关函数(二)
    三、依赖配置管理
    【宠物用品】宠物饮水机方案
    设计模式-状态模式在Java中的使用示例-信用卡业务系统
    Prophet模型的简介以及案例分析
    Flutter学习笔记 --单一子元素组件
    对线程池的理解
    分支对代码性能的影响和优化
    伦敦银现货市场如何使用多条均线?
    Nginx配置IP拦截
  • 原文地址:https://blog.csdn.net/guliguliguliguli/article/details/126200099