剑指 35

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

题目:

在这里插入图片描述

思路:

1.空间换时间,第一遍遍历,将next组装,并将Node,NodeCopy,放入map中,第二遍遍历时,关键代码root.random = map.get(head1.random);
1.1.自己的代码有点丑,看了题解,优化下自己的代码。思路是类似的,不过将第一步的next放到了第二步的构建中进行,变量也少了。 推荐!!!

2.不使用额外的空间。思路:
1.复制拼接
2.构建random
3.拆分(重点在拆分)
这个题的重点就是random是随机的,不能立刻获取到位置所在,所以思路一是放map,思路二就是将每个节点复制如:1,1copy,2,2copy……,这样在第二步时,原始的链表的random,的next就是我们复制节点的random。及相对位置是一样的,不过有个next,最后我们再进行拆分即可。明天写这个思路,现在有点头晕

题解

/*
// 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) {
        if(head == null) return null;
        HashMap<Node, Node> map = new HashMap<>();
        Node nodeCopy = new Node(head.val);
        Node head1 = head;
        Node root = nodeCopy;
        Node root1 = root;
        map.put(head, nodeCopy);
        head = head.next;
        while ( head != null) {
            nodeCopy.next = new Node(head.val);
            nodeCopy = nodeCopy.next;
            map.put(head, nodeCopy);
            head = head.next;
        }
        while (head1 != null) {
            root.random = map.get(head1.random);
            root = root.next;
            head1 = head1.next;
        }
        return root1;
    }
}

优化后:

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        HashMap<Node, Node> map = new HashMap<>();
        Node head1 = head;
        Node head2 = head;
        // 1.保存map
        while ( head != null) {
            Node node = new Node(head.val);
            map.put(head, node);
            head = head.next;
        }
        // 2.构建
        while (head1 != null) {
            map.get(head1).random = map.get(head1.random);
            map.get(head1).next = map.get(head1.next);
            head1 = head1.next;
        }
        return map.get(head2);
    }
}

使用第二种思路:

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        // 1.复制
        Node node = head;
        Node re = head;
        while (head != null) {
            Node head1 = new Node(head.val);
            head1.next = head.next;
            head.next = head1;
            head = head1.next;
        }
        // 2.random
        while (node != null) {
            if(node.random != null){
                node.next.random = node.random.next;
            }
            node = node.next.next;
        }
        // 3.拆分
        Node clone = re.next;
        Node nodeTemp = clone;
        re.next = clone.next;
        re = re.next;
        while (re != null) {
            clone.next = re.next;
            clone = clone.next;
            re.next = clone.next;
            re = re.next;
        }
        return nodeTemp;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值