复制复杂链表@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;
}
相关文章
|
1天前
|
索引
每日一题:力扣328. 奇偶链表
每日一题:力扣328. 奇偶链表
11 4
|
2天前
leetcode代码记录(移除链表元素
leetcode代码记录(移除链表元素
8 0
【每日一题】LeetCode——反转链表
【每日一题】LeetCode——反转链表
【每日一题】LeetCode——链表的中间结点
【每日一题】LeetCode——链表的中间结点
|
16天前
|
C++
[leetcode 链表] 反转链表 vs 链表相交
[leetcode 链表] 反转链表 vs 链表相交
|
25天前
【力扣】148. 排序链表
【力扣】148. 排序链表
|
25天前
|
索引
【力扣】142. 环形链表 II
【力扣】142. 环形链表 II
|
25天前
【力扣】19. 删除链表的倒数第 N 个结点
【力扣】19. 删除链表的倒数第 N 个结点
|
25天前
|
C语言 C++ 索引
【力扣】141. 环形链表、160. 相交链表、206.反转链表、234. 回文链表
【力扣】141. 环形链表、160. 相交链表、206.反转链表、234. 回文链表
|
2天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
6 0

热门文章

最新文章