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:

先一次生成每个节点对应的新节点,并用hashmap记录新旧节点之间的对应关系,然后再更新random指针

思路2:

一次生成每个旧节点的新节点,然后把新节点插入到旧节点的后继。

这样random直接的对应关系也就有了

new->random=old->random->next;

【代码】

/**
 * 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 {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        map<RandomListNode*, RandomListNode*> old2new;

		RandomListNode*newhead=NULL;
		RandomListNode*oldpointer=head;
		RandomListNode*newpointer=NULL;
		RandomListNode*prevnew=NULL;
		//生成新链表构造旧链表结点和新链表结点的对应关系
		while(oldpointer){
			newpointer=new RandomListNode(oldpointer->label);
			if(prevnew==NULL)newhead=newpointer;
			else{
				prevnew->next=newpointer;
			}
			prevnew=newpointer;

			old2new[oldpointer]=newpointer;
			oldpointer=oldpointer->next;
		}
		//更新random指针
		oldpointer=head;
		newpointer=newhead;
		while(oldpointer){
			newpointer->random=old2new[oldpointer->random];
			oldpointer=oldpointer->next;
			newpointer=newpointer->next;
		}
		return newhead;
    }
};

LeetCode: Copy List with Random Pointer [138],布布扣,bubuko.com

时间: 2024-10-07 06:00:04

LeetCode: Copy List with Random Pointer [138]的相关文章

[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 {//为了能够快速定位某个节点,采用确定性映射的方式,将

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; } 普通链表值拷贝 通常我们拷贝链表的时候,进行值拷贝,只用从头开始遍历就可以

【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

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