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;

}
相关推荐
逆小舟44 分钟前
【C/C++】指针
c语言·c++·笔记·学习
earthzhang20211 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
迎風吹頭髮1 小时前
UNIX下C语言编程与实践63-UNIX 并发 Socket 编程:非阻塞套接字与轮询模型
java·c语言·unix
奔跑吧邓邓子2 小时前
【C语言实战(6)】解锁C语言循环密码:for循环实战探秘
c语言·实战·for循环
GilgameshJSS2 小时前
STM32H743-ARM例程15-RTC
c语言·arm开发·stm32·实时音视频
pu_taoc2 小时前
深入剖析:基于epoll与主从Reactor模型的高性能服务器设计与实现
服务器·c语言·c++·vscode
冷徹 .5 小时前
2024ICPC区域赛香港站
数据结构·c++·算法
韧竹、5 小时前
数据结构之顺序表
c语言·数据结构
努力努力再努力wz10 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
凤年徐10 小时前
【C++】string的模拟实现
c语言·开发语言·c++