随机链表的复制数据结构oj题(力口138)

目录

问题描述

问题解读分析

解决代码


问题描述

问题解读分析

这里我们要注意深拷贝和随机指针 ,首先就是深拷贝对应的浅拷贝是和原链表用一个结点,而深拷贝则对应的不和原链表一个结点,也就是说需要我们自己开辟空间去创建一个结点和原链表一模一样。而这里随机指针,是链表中一个指针,他随机但是又固定,固定是因为我们不需要随机给他寻找结点,而是实例给,但是随机又不能琢磨他只能在相对结点的右边,而还有相对于这个结点的左边。所以这里最为复杂的是该如何处理random指针,这里很难寻找到相对结点靠左的结点和random对应,我们这里选择在原链表基础上进行拷贝,通过原链表上的random指针的next(这个表示在原链表上的下一个指针,这个指针就是复制的链表)与我们拷贝对应 。很抽象上图:

最后一步就是将原链表和复制好的链表断开

解决代码

cpp 复制代码
/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */
struct Node* buyNode(int x)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if(newnode == NULL)
    {
        perror("malloc");
        return NULL;
    }
    newnode->val = x;
    newnode->random= newnode->next = NULL;
    return newnode;
}

void AddNode(struct Node* phead)
{
    struct Node* pcur = phead;
    while(pcur)
    {
        struct Node* next = pcur->next;
        struct Node* newnode =buyNode(pcur->val);
        pcur->next = newnode;
        newnode->next = next;
        pcur = next;
    }
}
void setRandom(struct Node* phead)
{
    struct Node* pcur = phead;
    //根据newnode->random = pcur->random->next
    while(pcur)
    {
        struct Node* copy = pcur->next;
        if(pcur->random)//防止为空时解引用
        {
            copy->random = pcur->random->next;
        }
        pcur = copy->next;
    }
}
struct Node* copyRandomList(struct Node* head) {
	if(head == NULL)//防止没意义空指针的发生
    {
        return head;
    }
    //复制原链表
    AddNode(head);
    //复制random
    setRandom(head);
    //将原链表上的复制链表拆除
    struct Node* pcur = head;
    struct Node* copyHead=pcur->next;
    struct Node* copyTail = pcur->next;
    head->next = copyTail->next;//原链表链接
    while(pcur->next)
    {
        //copyTail --- pcur->next
        pcur = copyTail->next;
        copyTail->next = pcur->next;
        copyTail = copyTail->next;
        //pcur ----- pcur->next
        pcur->next = copyTail->next;
    } 
    return copyHead;
}
相关推荐
柒.梧.1 天前
数据结构:二叉排序树构建与遍历的解析与代码实现
java·开发语言·数据结构
zhuzewennamoamtf1 天前
Linux内核platform抽象、数据结构、内核匹配机制
linux·运维·数据结构
不穿格子的程序员1 天前
从零开始写算法——链表篇5:K个一组翻转链表 + 排序链表
算法·链表·分治
自然常数e1 天前
深入理解指针(6)
c语言·数据结构·算法·visual studio
一杯美式 no sugar1 天前
数据结构——栈
c语言·数据结构·
蒙奇D索大1 天前
【数据结构】考研408 | 冲突解决精讲: 拉链法——链式存储的艺术与优化
数据结构·笔记·考研·改行学it
一直都在5721 天前
数据结构入门:二叉排序树的构建与相关算法
数据结构·算法
_Minato_1 天前
数据结构知识整理——复杂度的计算
数据结构·经验分享·笔记·算法·软考
月明长歌1 天前
【码道初阶】【LeetCode 102】二叉树层序遍历:如何利用队列实现“一层一层切蛋糕”?
java·数据结构·算法·leetcode·职场和发展·队列
聆风吟º1 天前
【数据结构手札】顺序表实战指南(一):线性表定义 | 顺序表定义
数据结构·顺序表·线性表