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;

}
相关推荐
Syntech_Wuz1 小时前
从 C 到 C++:容器适配器 std::stack 与 std::queue 详解
数据结构·c++·容器··队列
艾莉丝努力练剑2 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
kyle~2 小时前
计算机系统---CPU的进程与线程处理
linux·服务器·c语言·c++·操作系统·计算机系统
小此方3 小时前
C语言自定义变量类型结构体理论:从初见到精通(下)
c语言·数据结构·算法
im_AMBER3 小时前
数据结构 05 栈和队列
数据结构·笔记·学习
_poplar_4 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
Yupureki4 小时前
从零开始的C++学习生活 7:vector的入门使用
c语言·c++·学习·visual studio
张口口4 小时前
1032 挖掘机技术哪家强
c语言
GawynKing5 小时前
红黑树-带源码
数据结构·算法·二叉树··红黑树
一语雨在生无可恋敲代码~5 小时前
leetcode724 寻找数组的中心下标
数据结构·算法