leetcode - Copy List with Random Pointer题解

链表格式

通常链表就是一个值,一个next指针,指向后面的节点。

结构体如下:

struct Node{
int val;
struct Node* next;
}

这个题目里的节点多了一个指针,除了指向下一个节点的next指针,还有一个指向这个链表随机一个节点的random指针。

结构体如下:

struct Node{
int val;
struct Node* next;
struct Node* random;
}

普通链表值拷贝

通常我们拷贝链表的时候,进行值拷贝,只用从头开始遍历就可以,因为节点与节点之间的关系已经确定了,每个节点都是指向后面的节点。直接遍历拷贝就可以了。

随机指针链表拷贝

随机链表的拷贝就是要把这个节点与节点之间的指向关系也同时拷贝。

O(n^2)的方法不写了,就是先遍历一遍,每次遍历都在链表中找random的指向的节点。

下面说一个O(n)的方法,就是依次创建节点,每个节点都插入到原链表的结尾。例如本来一个链表是:

      1  ->  2  ->  3  ->  4  ->  5  -> NULL
      (随机指针没有表示出来,因为不好画)

变成:

     1->1‘->2->2‘->3->3‘->4->4‘->5->5‘->NULL
         (新插入的节点都加了单引号来区分)

这样我们发现,假如1随机指针指向节点3,那么我们我们让1‘的随机指针指向3后面的3‘就可以了~因为我们可以通过1->random直接来访问3,所以直接让1‘->random
= 1->random->next
就可以了。这样就拷贝成功了。

最后一遍把这个新的链表再拿出来~就是让1‘->next = 1‘->next->next这样就可以了。

代码如下:

#include <iostream>
using namespace std;

struct RandomListNode{
    int label;
    RandomListNode *next, *random;
    RandomListNode(int x): label(x), next(NULL), random(NULL){}
};

class Solution{
public:
    RandomListNode * copyRandomList(RandomListNode *head){
        RandomListNode *node;
        RandomListNode *loop = head;
        if (loop == NULL) {
            return  NULL;
        }
        while (loop != NULL) {
            node = (RandomListNode*)malloc(sizeof(struct RandomListNode));
            node->label = loop->label;
            node->next = loop->next;
            node->random = NULL;
            loop->next = node;
            loop = node->next;
        }
        loop = head;
        while (loop != NULL) {
            if (loop->random != NULL) {
                loop->next->random = loop->random->next;
            }
            loop = loop->next->next;
        }
        RandomListNode *copy = head->next;
        loop = head;
        node = head->next;
        while (loop != NULL && node->next != NULL) {
            loop->next = loop->next->next;
            node->next = node->next->next;
            loop = loop->next;
            node = node->next;
        }
        loop->next = NULL;
        return copy;
    }

};
时间: 2024-10-13 02:42:50

leetcode - Copy List with Random Pointer题解的相关文章

LeetCode: Copy List with Random Pointer [138]

[题目] A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

[leetcode]Copy List with Random Pointer @ Python

原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 解题思路:这题主要是需要深

LeetCode——Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目:给定一个链表,当中的每一个节

[LeetCode]Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 利用hashMap存储,原始的node作为key,new的node作为value,这样就建立了新旧链表的以一一对应关系: /** * Definition for sing

[LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 这道链表的深度拷贝题的难点就在于如何处理随机指针的问题,由于每一个节点都有一个随机指针,这个指针可以为空,也可以指向链表的任意一个节点,如果我们在每生成一个新节点给其随机指

LeetCode&mdash;&mdash;Copy List with Random Pointer(带random引用的单链表深拷贝)

问题: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.   结点的定义如下: /** * Definition for singly-linked list with a random pointer. * class

[LeetCode]Copy List with Random Pointer复杂链表的复制

/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */ class Solution {//为了能够快速定位某个节点,采用确定性映射的方式,将

Copy List with Random Pointer leetcode java

题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n