题解 | #复杂链表的复制#

复杂链表的复制

https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba

/**
 * struct RandomListNode {
 *  int label;
 *  struct RandomListNode *next;
 *  struct RandomListNode *random;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pHead RandomListNode类
 * @return RandomListNode类
 */
struct RandomListNode* Clone(struct RandomListNode* pHead ) {
    // write code here
    struct RandomListNode* f(struct RandomListNode * pHead,
                             struct RandomListNode * flag[]);
    struct RandomListNode* flag[1001] = {NULL};
    return f(pHead, flag);
}
struct RandomListNode* f(struct RandomListNode* pHead,
                         struct RandomListNode* flag[]) {
    if (flag[pHead->label] || !pHead) return NULL;
    // write code here
    struct RandomListNode* result = (struct RandomListNode*)malloc(sizeof(
                                        struct RandomListNode));
    flag[pHead->label] = result;
    result->next = NULL;
    result->random = NULL;
    result->label = pHead->label;
    if (pHead->next) {
        result->next = f(pHead->next, flag);
    }
    if (pHead->random) {
        if (flag[pHead->random->label]) {
            result->random = flag[pHead->random->label];
        } else {
            result->random = f(pHead->random, flag);
        }
    }

    return result;
}

全部评论

相关推荐

昨天 15:52
东南大学 C++
点赞 评论 收藏
分享
04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务