首页 > 其他分享 >5.复杂链表的深度拷贝(Leetcode 138)

5.复杂链表的深度拷贝(Leetcode 138)

时间:2023-02-09 00:35:57浏览次数:42  
标签:node head RandomListNode random Leetcode 链表 next 138 ptr

5.复杂链表的深度拷贝(Leetcode 138)
	
#include <stdio.h>
		
struct RandomListNode {
	int label;
	RandomListNode *next, *random;
	RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

#include <map>
#include <vector>

class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
    	std::map<RandomListNode *, int> node_map;
    	std::vector<RandomListNode *> node_vec;
    	RandomListNode *ptr = head;
    	int i = 0;
    	while (ptr){
	    	node_vec.push_back(new RandomListNode(ptr->label));
	    	node_map[ptr] = i;
	    	ptr = ptr->next;
	    	i++;
	    }
	    node_vec.push_back(0);
	    ptr = head;
	    i = 0;
	    while(ptr){
    		node_vec[i]->next = node_vec[i+1];
    		if (ptr->random){
    			int id = node_map[ptr->random];
		    	node_vec[i]->random = node_vec[id];
		    }
    		ptr = ptr->next;
    		i++;
    	}
    	return node_vec[0];
    }
};

int main(){
	RandomListNode a(1);
	RandomListNode b(2);
	RandomListNode c(3);
	RandomListNode d(4);
	RandomListNode e(5);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;	
	a.random = &c;
	b.random = &d;
	c.random = &c;
	e.random = &d;	
	Solution solve;
	RandomListNode *head = solve.copyRandomList(&a);	
	while(head){
		printf("label = %d ", head->label);
		if (head->random){
			printf("rand = %d\n", head->random->label);
		}
		else{
			printf("rand = NULL\n");
		}
		head = head->next;
	}
	return 0;
}

标签:node,head,RandomListNode,random,Leetcode,链表,next,138,ptr
From: https://www.cnblogs.com/Epiephany/p/17103839.html

相关文章

  • 6.链表求环||(Leetcode 42)
    6.链表求环||(Leetcode42)方法一:#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};#include<set>cl......
  • 7.两个排序链表的交点(Leetcode 160)
    7.两个排序链表的交点(Leetcode160)方法一:#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};#include......
  • 8.翻转链表(Leetcode206)
    8.翻转链表(Leetcode206)#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{public:......
  • 二分查找 Leetcode704
    1.二分查找(Leetcode704)题目:给定一个n(n在[1,10000]之间)个元素有序的(升序)整型数组nums和一个目标值target,写一个函数搜索nums中的target,如果目标值存在返回下标,否......
  • 在排序数组中查找元素的第一个和最后一个位置(Leetcode34)
    3.在排序数组中查找元素的第一个和最后一个位置(Leetcode34)给定一个按照升序排列的整数数组nums,和一个目标值target。找出给定目标值在数组中的开始位置和结束位置。如......
  • 插入搜索位置(Leetcode35)
    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。题目解析:元素......
  • 链表基础
    二、链表:初识链表基础知识:什么是链表:链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点......
  • 1.两个排序链表归并(Leetcode 21)
    1.两个排序链表归并(Leetcode21)#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{p......
  • 【LeetCode】三数之和+四数之和(双指针)
    之所以放在一起是因为,"四数之和"的解题方法基本与"三数之和"一致由此我们可以推出n数之和的解法本质上,我们只是使用双指针的方法降低此类问题的时间复杂度当然用哈希法......
  • #yyds干货盘点# LeetCode程序员面试金典:颜色填充
    题目:编写函数,实现许多图片编辑软件都支持的「颜色填充」功能。待填充的图像用二维数组image表示,元素为初始颜色值。初始坐标点的行坐标为sr列坐标为sc。需要填充的新颜......