LeetCode题解之Copy List with Random Pointer

1、题目描述

2、问题分析

首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可。

3、代码

 1 RandomListNode *copyRandomList(RandomListNode *head) {
 2        if( head == NULL){
 3            return NULL;
 4        }
 5         RandomListNode* newhead = new RandomListNode(0);
 6         RandomListNode* np = newhead;
 7
 8         RandomListNode* p = head;
 9         map<RandomListNode*, RandomListNode*> m;
10         while (p != NULL){
11             RandomListNode* tmp = new RandomListNode(p->label);
12             m.insert(make_pair(p,tmp));
13             np->next = tmp;
14             np = np->next;
15             p = p->next;
16         }
17
18         p = head ;
19         np = newhead->next;
20         while( p != NULL){
21             np->random = m[p->random];
22             p = p->next;
23             np = np->next;
24         }
25         return newhead->next;
26
27     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/9573608.html

时间: 2024-07-31 02:44:10

LeetCode题解之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. 思路: 主要是深层复制的问题: 本题比较简单,具体实现见代码: /** * Definition for singly-linked list with a ran

LeetCode OJ: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. 这题用map做其实比较简单,但是一开始没想明白越想越乱,最后看了下别人的实现,思路还是很清晰的,代码如下所示: 1 /** 2 * Definition for singl

【LeetCode】138. 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. 提示: 此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第

leetcode || 138、Copy List with Random Pointer

problem: 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. Hide Tags Hash Table Linked List 题意:copy一个单链表,单链表的节点多了一个指针,随机指向一个节点或者空 thinki

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

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][JavaScript]Copy List with Random Pointer

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://leetcode.com/problems/copy-list-with-random-poin