【链表OJ】Leetcode 138. 复制带随机指针的链表(集大成者)

题目链接

解题思路

此题可以分为三步进行:

  1. 拷贝链表的每一个结点,拷贝的结点先链接到被拷贝结点后面
  2. 复制随机指针的链接:拷贝结点的随机指针指向被拷贝结点随机指针的下一个位置
  3. 拆解链表,把拷贝的链表从原链表中拆解出来

第一步

拷贝链表的每一个结点,拷贝的结点先链接到被拷贝结点后面。

在这里插入图片描述
需要注意 copy 结点插入时,指针先后顺序。避免结点 脱钩。

// 将新结点插入到原链表每一个结点后面
    struct Node* cur = head;
    while(cur)
    {
        struct Node* copy = BuyListNode(cur->val);
        struct Node* next = cur->next;
        
        copy->next = next;
        cur->next = copy;
        // 迭代
        cur = next;
    }

第二步

复制随机指针的链接:拷贝结点的随机指针指向被拷贝结点随机指针的下一个位置。

注意看蓝色画图部分,即为如何查找到random指针。

在这里插入图片描述
最终链接结果

在这里插入图片描述

// 根据原结点的random,处理复制结点的random
    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next, *next = copy->next;
        if(cur->random == NULL)
            copy->random = NULL;
        else
            copy->random = cur->random->next;

        cur = next;
    }

第三步

拆解链表,把拷贝的链表从原链表中拆解出来。

这里使用不带哨兵位的链表构造方法带哨兵位讲解指路 :分割链表
在这里插入图片描述

// 解绑新结点,恢复旧链表连接关系
    struct Node* newhead, *newtail;
    newhead = newtail = NULL;

    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next, *next = copy->next;

        // 对新链
        if(newhead == NULL)
        {
            newhead = copy;
            newtail = copy;
        }
        else
        {
            newtail->next = copy;
            newtail = copy;
        }

        // 对旧链
        cur->next = next;
        cur = next;
    }

具体实现思路和尾插结点无异,不会的可以看这一篇 不带哨兵位单向链表

AC程序

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */
struct Node* BuyListNode(int x) {
    struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
    copy->val = x;
    copy->random = NULL;
    copy->next = NULL;
    return copy;
}

struct Node* copyRandomList(struct Node* head) {
	// 将新结点插入到原链表每一个结点后面
    struct Node* cur = head;
    while(cur)
    {
        struct Node* copy = BuyListNode(cur->val);
        struct Node* next = cur->next;
        copy->next = next;
        cur->next = copy;
        cur = next;
    }

    // 根据原结点的random,处理复制结点的random
    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next, *next = copy->next;
        if(cur->random == NULL)
            copy->random = NULL;
        else
            copy->random = cur->random->next;

        cur = next;
    }

    // 解绑新结点,恢复旧链表连接关系
    struct Node* newhead, *newtail;
    newhead = newtail = NULL;

    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next, *next = copy->next;

        // 对新链
        if(newhead == NULL)
        {
            newhead = copy;
            newtail = copy;
        }
        else
        {
            newtail->next = copy;
            newtail = copy;
        }

        // 对旧链
        cur->next = next;
        cur = next;
    }

    return newhead;
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JoyCheung-

赏颗糖吃吧~~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值