复制复杂链表@Leetcode —— 单链表

简介: 复制复杂链表

@TOC

1. 题目

题目链接:复制带随机指针的链表
在这里插入图片描述

2. 思路

这道题目的难点在于 :random指针也都应指向复制链表中的新节点

这思路也太妙了吧!
:star: 1. 复制节点,插入在原节点和下一个节点之间
在这里插入图片描述

:star: 2. 根据原节点的random,处理复制节点的random
:star: 3. 把复制节点解下来,链接成一个新链表,恢复原链表的链接关系。

3. 题解

struct Node* copyRandomList(struct Node* head) {
    if(head == NULL)
        return NULL;
    // 1.复制节点,插入到原节点和下一个节点之间
    struct Node* cur = head;
    while(cur)
    {
        struct Node* copynode = (struct Node*)malloc(sizeof(struct Node));
        copynode->val = cur->val;

        struct Node* next = cur->next;
        //链接
        cur->next = copynode;
        copynode->next = next;
        cur = next;
    }
    // 2. 根据源节点的random处理复制节点的random
    cur = head;
    while(cur)
    {
        if(cur->random == NULL)
        {
            cur->next->random = NULL;
        }
        else
        {
            cur->next->random = cur->random->next;
        }
        cur = cur->next->next;
    }
    // 3.复制节点解下来缝合成新链表,缝合原链表链接关系
    cur = head;
    struct Node* copyhead = head->next;
    while(cur)
    {
        struct Node* copynode = cur->next;
        struct Node* next = copynode->next;
        cur->next = next; //缝合
        //链接
        if(next == NULL) 
            copynode->next = NULL;
        else
            copynode->next = next->next;

        // 迭代
        cur = next;
    }
    return copyhead;
}

这是另一份参考代码,差别主要是在第三部分,它采取尾插构建新链表,我直接在2的结构上缝合。

struct Node* copyRandomList(struct Node* head) {
    struct Node* cur = head;

    //1.拷贝节点,插入到原节点的后面
    while(cur)
    {
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        //插入copy节点
        copy->next = cur->next;
        cur->next = copy;
        //迭代着向后走
        cur = copy->next;
    }
    //2.根据原节点,处理copy节点的random
    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next;
        if(cur->random == NULL)
        {
            copy->random = NULL;
        }
        else
        {
            copy->random = cur->random->next;
        }
        cur = copy->next;
    }
    //3.把拷贝节点解下来,链接成新链表(尾插链接),同时恢复原链表
    struct Node *copyHead = NULL,*copyTail = NULL;//不需要每次都找尾
    cur = head;
    while(cur)
    {
        struct Node* copy = cur->next;
        struct Node* next = copy->next;
        if(copyTail == NULL)
        {
            //第一个
            copyHead = copyTail = copy;
        }
        else
        {
            //尾插
            copyTail->next = copy;
            copyTail = copy;//这样的话更新
        }
        //恢复原链表
        cur->next = next;

        cur = next;
    }
    return copyHead;
}
相关文章
|
6天前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
16 1
|
13天前
Leetcode第21题(合并两个有序链表)
这篇文章介绍了如何使用非递归和递归方法解决LeetCode第21题,即合并两个有序链表的问题。
37 0
Leetcode第21题(合并两个有序链表)
|
13天前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
12 0
LeetCode第二十四题(两两交换链表中的节点)
|
13天前
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第19题要求删除链表的倒数第N个节点,可以通过快慢指针法在一次遍历中实现。
32 0
Leetcode第十九题(删除链表的倒数第N个节点)
|
13天前
|
索引
力扣(LeetCode)数据结构练习题(3)------链表
力扣(LeetCode)数据结构练习题(3)------链表
27 0
|
13天前
【LeetCode 10】142. 环形链表 II
【LeetCode 10】142. 环形链表 II
16 0
|
13天前
【LeetCode 09】19 删除链表的倒数第 N 个结点
【LeetCode 09】19 删除链表的倒数第 N 个结点
12 0
|
13天前
【LeetCode 08】206 反转链表
【LeetCode 08】206 反转链表
11 0
|
13天前
【LeetCode 06】203.移除链表元素
【LeetCode 06】203.移除链表元素
26 0
|
1月前
|
存储 算法 C语言
C语言手撕实战代码_循环单链表和循环双链表
本文档详细介绍了用C语言实现循环单链表和循环双链表的相关算法。包括循环单链表的建立、逆转、左移、拆分及合并等操作;以及双链表的建立、遍历、排序和循环双链表的重组。通过具体示例和代码片段,展示了每种算法的实现思路与步骤,帮助读者深入理解并掌握这些数据结构的基本操作方法。