【Java - J - 35】复杂链表的复制

题目描述

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

lc
练习地址

L - 0138】- m - 复制带随机指针的链表

实现

代码最后一段不太好理解,参考lc的

复杂度O(n),不使用辅助空间
在这里插入图片描述
第一步:根据原始链表的每个节点N创建N’,直接放在N后。
在这里插入图片描述
第二步:设置复制的节点的特殊指针。存在N->S,则N’->S’。
在这里插入图片描述
第三步,按奇偶位将长链表茶分为两个链表
在这里插入图片描述

public class C35_list_CloneList {
    public static class RandomListNode {
        int val;
        RandomListNode next = null;
        RandomListNode random = null;
        RandomListNode(int val) {
            this.val = val;
        }
    }
    
    public static RandomListNode Clone(RandomListNode head) {
        if (head == null) {
            return null;
        }

        RandomListNode node = head;
        //1、复制每个结点,如复制结点A得到A1,将结点A1插到结点A后面;
        while (node != null) {
            RandomListNode cloned = new RandomListNode(node.val);
            RandomListNode next = node.next;
            node.next = cloned;
            cloned.next = next;
            node = next;
        }

        node = head;
        //2、重新遍历链表,复制老结点的随机指针给新结点,如A1.random = A.random.next;
        while (node != null) {
            node.next.random = node.random == null ? null : node.random.next;//node.next为克隆节点
            node = node.next.next;
        }

        //3、拆分链表,将链表拆分为原链表和复制后的链表
        node = head;
        RandomListNode cloneHead = head.next;
        while (node != null) {
            RandomListNode cloneNode = node.next;
            node.next = cloneNode.next;
            cloneNode.next = cloneNode.next == null ? null : cloneNode.next.next;
            node = node.next;
        }
        return cloneHead;
    }
}
Test
public static void main(String[] args) {
        RandomListNode node1 = new RandomListNode(1);
        RandomListNode node2 = new RandomListNode(2);
        RandomListNode node3 = new RandomListNode(3);
        RandomListNode node4 = new RandomListNode(4);
        RandomListNode node5 = new RandomListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node1.random = node3;
        node2.random = node5;
        node4.random = node2;

        RandomListNode head = Clone(node1);
        while (head != null) {
            System.out.print(head.val);
            if (head.random != null) {
                System.out.print(" " + head.random.val);
            }
            head = head.next;
            System.out.println();
        }
    }
输出
1 3
2 5
3
4 2
5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值