复杂链表的复制

本文介绍了一种复制复杂链表的方法,该链表包含next和random两种指针。通过在原链表节点后插入复制节点,并调整random指针,最终分离得到完整复制的链表。该算法时间复杂度为O(n),空间复杂度为O(1)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的头结点。
思路:
1. 首先根据链表的next遍历一遍,在每个node节点的后面复制一个与node的label相等的节点,如图所示;
2. 再遍历新链表,为复制的节点指定random指针(黄色箭头所示);
3. 将新链表拆分=原链表+复制后的链表
4. 在操作过程中一定要主要判断节点是否为空
5. 时间复杂度o(n),空间复杂度o(n)
这里写图片描述
示例代码如下:

/**
 * Copyright (C), 2017
 * FileName: Main
 * Author:   Mengjun Li
 * Date:     2017/10/3 上午11:09
 * Description:
 */

/**
 * @author Mengjun Li
 * @create 2017/10/3
 * @since 1.0.0
 */

public class Main {
    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);
        RandomListNode node6 = new RandomListNode(6);
        node1.next = node2;
        node1.random = node3;

        node2.next = node3;
        node2.random = node4;

        node3.next = node4;
        node3.random = node1;

        node4.next = node5;
        node4.random = node2;

        node5.next = node6;
        node5.random = null;

        node6.random = node5;

        System.out.println("原链表的next指针");
        while (head != null) {
            System.out.print(head.label + "-->");
            head = head.next;
        }
        System.out.println("null");

        //Clone链表
        head = Clone(node1);

        System.out.println("clone的链表的next指针");
        while (head != null) {
            System.out.print(head.label + "-->");
            head = head.next;
        }
        System.out.println("null");

        System.out.println("原链表的random指针");
        head = node1;
        while (head != null) {
            if (head.random != null)
                System.out.print(head.label + "-->"+head.random.label+", ");
            else
                System.out.print(head.label + "--> null, ");
            head = head.next;
        }
        System.out.println("");

        head = Clone(node1);
        System.out.println("clone的链表的random指针");
        while (head != null) {
            if (head.random != null)
                System.out.print(head.label + "-->"+head.random.label+", ");
            else
                System.out.print(head.label + "--> null, ");
            head = head.next;
        }

    }

    public static RandomListNode Clone(RandomListNode pHead) {
        if (pHead == null)
            return pHead;
        RandomListNode tmp = null, oHead = pHead, newNode;
        //第一步:在原链表中每一个节点的后面复制前面一个节点
        while (oHead != null) {
            newNode = new RandomListNode(oHead.label);
            tmp = oHead.next;
            oHead.next = newNode;
            newNode.next = tmp;
            oHead = tmp;
        }
        oHead = pHead;
        //第二步:给复制的节点的random指针赋值
        while (oHead != null) {
            if (oHead.random != null)//random可能为null
                oHead.next.random = oHead.random.next;
            else
                oHead.next.random = null;
            oHead = oHead.next.next;
        }
        //将复制后的链表和原链表断开
        oHead = pHead;
        newNode = pHead.next;
        while (oHead != null) {
            tmp = oHead.next;
            if (tmp != null)
                oHead.next = tmp.next;
            oHead = tmp;
        }
        return newNode;
    }

}

class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}

输出如下:

原链表的next指针
1-->2-->3-->4-->5-->6-->null
clone的链表的next指针
1-->2-->3-->4-->5-->6-->null
原链表的random指针
1-->32-->43-->14-->25--> null, 6-->5, 
clone的链表的random指针
1-->32-->43-->14-->25--> null, 6-->5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值