LeetCode-138. 复制带随机指针的链表-Java-medium

题目链接

Node
public class Node {
    public int val;
    public Node next;
    public Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
创建带头节点的随机指针链表
    /**
     * 创建带头节点的随机指针链表
     *
     * @param arr
     * @return
     */
    public Node creatRandomNextList(Integer[][] arr) {
        Node head = new Node(0); // 头节点
        if (arr.length == 0) {
            return head;
        }
        Node curNode = head;
        Map<Integer, Node> mp = new HashMap<>(); // 存储节点索引与节点之间的映射
        for (int i = 0; i < arr.length; i++) {   // 构建节点,填充next指针,构建映射
            Node newNode = new Node(arr[i][0]);
            curNode.next = newNode;
            curNode = curNode.next;
            mp.put(i, curNode);
        }
        curNode.next = null;
        curNode = head.next;
        for (int i = 0; i < arr.length; i++) {   // 通过映射关系填充random指针
            curNode.random = mp.get(arr[i][1]);
            curNode = curNode.next;
        }
        return head;
    }
打印不带头节点的随机指针链表
    /**
     * 打印不带头节点的随机指针链表
     *
     * @param head
     */
    public void showNoHeadRandomNextList(Node head) {
        Node curNode = head;
        while (curNode != null) {
            Integer randomVal = curNode.random == null ? null : curNode.random.val;
            System.out.print("[" + curNode.val + " " + randomVal + "] ");
            curNode = curNode.next;
        }
        System.out.println();
    }
法一(哈希)
    /**
     * 法一(哈希)
     * 时间复杂度O(n)
     * 空间复杂度O(n)
     *
     * @param head
     * @return
     */
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        Node curNode = head;
        Map<Node, Node> mp = new HashMap<>(); // 利用hash存储原结点和克隆结点的映射关系
        while (curNode != null) {
            Node copyNode = new Node(curNode.val);
            mp.put(curNode, copyNode);
            curNode = curNode.next;
        }
        curNode = head;
        while (curNode != null) { // 通过映射关系处理克隆结点的next指针和random指针
            mp.get(curNode).next = mp.get(curNode.next);
            mp.get(curNode).random = mp.get(curNode.random);
            curNode = curNode.next;
        }
        return mp.get(head);
    }
本地测试
        /**
         * 138. 复制带随机指针的链表
         */
        lay.showTitle(138);
        Solution138 sol138 = new Solution138();
        Integer[][] arr138 = new Integer[][]{{7, null}, {13, 0}, {11, 4}, {10, 2}, {1, 0}};
        randomNextList.Node head138 = randomNextListOpt.creatRandomNextList(arr138);
        randomNextListOpt.showNoHeadRandomNextList(head138.next);
        randomNextList.Node copyHead138 = sol138.copyRandomList(head138);
        randomNextListOpt.showNoHeadRandomNextList(copyHead138.next);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值