栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

链表的深拷贝

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链表的深拷贝

文章目录
  • 链表
    • 1.带随机指针的链表深拷贝
      • 1.1.哈希表
      • 1.2 原地修改

链表

​ 字节等大厂面试的时候可能会要自己写题目要求地方数据结构。

// 自己定义链表节点
class Node{
    int val;
    Node next;
    Node random;
    
    public Node(int val){
        this.val = val;
        // 进行初始化!
        this.next = null;
        this.random = null;
    }
}
1.带随机指针的链表深拷贝

剑指 Offer 35. 复杂链表的复制

​ 链表深拷贝,纯本人菜鸡手写。面试官可能并 不希望 你使用哈希表。

1.1.哈希表
class Solution {
    public Node copyRandomList(Node head) {
        if(head==null) return null;
        // 第一次复制 整个链表
        Map map = new HashMap<>();
        Node res = new Node(head.val);
        Node temp = head;
        Node temp1 = res;
        //  复制单独的链表
        map.put(temp,temp1);
        while(temp.next!=null){
            temp = temp.next;
            Node nextn = new Node(temp.val);       
            temp1.next =nextn;
            temp1 = temp1.next;
            // 链表保存 节点
            // list.add(temp1); 
            map.put(temp,temp1);
        }
        // 复制random
        temp = head;
        temp1 = res;
        if(temp.random==null){
            temp1.random =null;
        }else{
            temp1.random =  map.get(temp.random);
        }       
        while(temp1.next!=null){
            temp = temp.next;
            temp1 = temp1.next;
            if(temp.random==null){
                temp1.random =null;
            }else{
                temp1.random =  map.get(temp.random);
            }

        }

        return res;
        
    }
}

参考其他大佬的哈希表写法


class Solution {
    public Node copyRandomList(Node head) {
        //边界条件
        if(head==null) return null;    
        //实现遍历链表
        Node node = head;
        Map map = new HashMap<>();
		//把源链表和其复制的空链表节点放入哈希表;
        //便于对next和random两个指针进行操作;
        while(node!=null){
            Node temp = new Node(node.val,null,null);
            map.put(node,temp);
            node = node.next;
        }
        node = head;
		//第二次遍历,将指针进行复制
        while(node!=null){
            map.get(node).next = map.get(node.next);        
            //这一步就是深拷贝的关键点,直接拷贝得到的rangdom指针还是指向原链表的!!!          
            //这一点理解了就OK!
            map.get(node).random = map.get(node.random);
            node = node.next;
        }
        return map.get(head);  
    }
}
1.2 原地修改

​ 这个应该是面试官想要看到的答案。

class Solution {
    public Node copyRandomList(Node head) {
        if(head==null) return null;
        Node cur = head;

        // 在初始列表上复制
        while(cur != null) {
            Node temp = new Node(cur.val);
            // 把复制后的节点插入到当前节点的后面
            temp.next = cur.next;
            cur.next = temp;
            cur = temp.next;
        }

        cur = head;
        // 操作random
        while(cur!=null){
            if(cur.random!=null){
                // 指向random的下一个,就是复制后的random
                cur.next.random = cur.random.next;
            }
            cur = cur.next.next;
        }
        // 3. 拆分
        cur= head;//拆原链表的工具
        Node res = head.next;
        Node temp = head.next;//拆新链表的工具

        while(temp.next!=null){
            // 这个顺序错了会报错,一定先操作前面的,按照先后顺序。
            cur.next = cur.next.next;
            temp.next = temp.next.next;
            cur = cur.next;
            temp = temp.next;
           
        }

        cur.next =null;
        return res;
        
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号