坚持每天一道力扣题_复杂链表的复制

⭐️坚持每天一道力扣题

复杂链表的复制

在这里插入图片描述
在这里插入图片描述

方法:拼接 + 拆分
考虑构建 原节点 1 -> 新节点 1 -> 原节点 2 -> 新节点 2 -> …… 的拼接链表,如此便可在访问原节点的random指向节点的同时找到新对应新节点的 random 指向节点。
算法流程

  1. 复制各节点,构建拼接链表:
  • 设原链表为 node1→node2→⋯ ,构建的拼接链表如下所示:

    n o d e 1 → n o d e 1   n e w   → n o d e 2 → n o d e 2   n e w   → ⋯ node1→node1~new~ →node2→node2~new~ →⋯ node1node1 new node2node2 new 

  1. 构建新链表各节点的 random 指向:
  • 当访问原节点 cur 的随机指向节点cur.random时,对应新节点 cur.next 的随机指向节点为 cur.random.next
  1. 拆分原 / 新链表:
  • 设置 pre / cur 分别指向原 / 新链表头节点,遍历执行pre.next = pre.next.nextcur.next = cur.next.next 将两链表拆分开。
    返回新链表的头节点 ret 即可。

    在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == NULL)
        {
            return NULL;
        }
        Node* cur = head;
        while(cur)
        {
            //1、创建新节点插入在原节点后面
             Node* copy = (Node*)malloc(sizeof(Node));
             copy->val = cur->val;
             //插入copy节点
             copy->next = cur->next;
             cur->next = copy;
             cur = copy->next;
        }
        //2、根据原节点,处理原节点的radom
        cur = head;
        while(cur)
        {
            Node* copy = cur->next;
            if(cur->random == NULL)
                copy->random = NULL;
            else
                copy->random = cur->random->next;
            cur = copy->next;
        }
        Node* copyHead = NULL, *copyTail = NULL;
        cur = head;
        while(cur)
        {
            Node* copy = cur->next;
            Node* next = copy->next;
            if(copyTail == NULL)
            {
                copyHead = copyTail = copy;
            }
            else
            {
                copyTail->next = copy;
                copyTail = copy;
            }
            //恢复原链表
            cur->next = next;
            cur = next;
        }
        return copyHead;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值