C语言每日一题:14《数据结构》复制带随机指针的链表

题目一:


题目链接:

思路一:

找相对位置暴力求解的方法:

1.复制一个新的链表出来遍历老的节点给新的节点赋值,random这个时候不去值。

2.两个链表同时遍历,遍历老链表的时候去寻找相对位置,在遍历新的链表找到随机值赋值。

c 复制代码
struct Node* copyRandomList(struct Node* head) {
        struct Node* cur=head;
        struct Node* newhead=NULL,*tile=NULL;

        //复制原来的链表数据
        while(cur)
        {
            //开辟新的节点
            struct Node* newnode=(struct Node*)malloc(sizeof(struct Node));
            newnode->val=cur->val;
            newnode->next=NULL;
            newnode->random=NULL;


            if(newhead==NULL)
            {
                tile=newhead=newnode;
            }
            else
            {
                tile->next=newnode;
                tile=tile->next;
            }

            cur=cur->next;
        }

        //进行两个的循环遍历,找相对位置
        cur=head;
        struct Node* cur2=newhead;
        int pos=0;
        while(cur)
        {
            //更新一下pos
            pos=0;
            //cur的随机值是哪一个
            struct Node* find=cur->random;
            if(find==NULL)
            {
                cur2->random=NULL;
                cur=cur->next;
                cur2=cur2->next;
                continue;
            }
            else
            {
                struct Node* curold=head;
                while(curold)
                {
                    if(find==curold)
                    {
                        break;
                    }
                    pos++;
                    curold=curold->next;
                }
            }

            //寻找随机节点
            struct Node* curnew=newhead;
            while(pos)
            {
                curnew=curnew->next;
                pos--;
            }
             cur2->random=curnew;


            //循环条件
            cur=cur->next;
            cur2=cur2->next;

        }

        return newhead;
}

思路二:

c 复制代码
struct Node* copyRandomList(struct Node* head) {

    struct Node* cur = head, * tile = NULL;
    //新的链表赋值插入,cur为空才结束插入
    while (cur)
    {
        //保存下一个老的
        tile = cur->next;

        struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
        newnode->val = cur->val;
        cur->next = newnode;
        newnode->next = tile;

        //循环条件
        cur = tile;
    }

    //给copy链表赋值random
    struct Node* copy = NULL;
    cur = head;
    tile = NULL;
    while (cur)
    {
        //连接了新的节点
        copy = cur->next;
        tile = copy->next;

        //给random赋值,随机值,正常值的两个情况
        if (cur->random == NULL)
        {
            copy->random = NULL;
        }
        else
        {
            copy->random = cur->random->next;
        }

        //循环的移动
        cur = tile;
    }

    copy = NULL;
    cur = head;
    tile = NULL;
    //分离链表
    struct Node* newhead = NULL;
    struct Node* move = NULL;

    while (cur)
    {
        copy = cur->next;
        tile = copy->next;

        if (newhead == NULL)
        {
            newhead = copy;
            move = newhead;
        }
        else
        {
            move->next = copy;
            move = move->next;
        }

        //恢复原来的节点
        cur->next = tile;

        //循环遍历
        cur = tile;
    }
    return newhead;

}
相关推荐
YuforiaCode3 分钟前
第十二届蓝桥杯 2021 C/C++组 卡片
c语言·c++·蓝桥杯
乌鸦94443 分钟前
《数据结构之美--二叉树》
数据结构·#二叉树
蒟蒻小袁1 小时前
力扣面试150题--K 个一组翻转链表
leetcode·链表·面试
阿方.9182 小时前
C语言----操作符详解(万字详解)
c语言·开发语言
夜晚中的人海2 小时前
【C语言】初阶算法相关习题(二)
c语言·开发语言·算法
一直很很安静3 小时前
java借助NIO、链表、跳表模拟实现redis
java·链表·nio
神一样的老师3 小时前
使用 MQTT - C 访问 IoTDA 平台:一个完整的嵌入式示例
c语言·开发语言·物联网
泽02023 小时前
数据结构之排序
数据结构·算法·排序算法
How_doyou_do4 小时前
项目实战-贪吃蛇大作战【补档】
c语言·c++·visual studio
Brookty4 小时前
【数据结构】二叉搜索树
数据结构