当前位置:网站首页>[force buckle] copy the linked list with random pointer
[force buckle] copy the linked list with random pointer
2022-07-19 06:25:00 【Patrick star`】
138. Copy list with random pointer - Power button (LeetCode)
subject :
Give you a length of n The linked list of , Each node contains an additional random pointer random , The pointer can point to any node or null node in the list .
To construct this linked list Deep copy . The deep copy should be made exactly by n individual new Node composition , The value of each new node is set to the value of its corresponding original node . New nodes next Pointers and random The pointer in the list should also point to the new node , And make these pointers in the original linked list and the copied linked list represent the same linked list state . The pointer in the copy linked list should not point to the node in the original linked list .
Ideas :

Code :
struct Node* copyRandomList(struct Node* head)
{
struct Node* cur = head;
struct Node* copyHead = NULL;
if(head == NULL)// The original linked list is empty
{
return copyHead;
}
while(cur)
{
struct Node* copyNode = (struct Node*)malloc(sizeof(struct Node));
copyNode->val = cur->val;
copyNode->next = cur->next;
cur->next = copyNode;
cur = cur->next->next;
}
copyHead = head->next;
cur = head;
while(cur)
{
if(cur->random)
{
cur->next->random = cur->random->next;
}
else
{
cur->next->random = NULL;
}
cur = cur->next->next;
}
struct Node* temp = NULL;
cur = copyHead;
while(cur->next)
{
temp = cur->next->next;
cur->next = temp;
cur = temp;
}
return copyHead;
}边栏推荐
猜你喜欢

Basic mathematics course 2_ Euler function, linear sieve, extended Euler

Résoudre le problème de l'ambiguïté de la cible dans l'interaction de fixation 3D par l'estimation de la profondeur vor

用头部运动学习无姿态注视偏差

【力扣】用栈实现队列

Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe

【力扣】复制带随机指针的链表

解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...

What kind of deep learning is most suitable for your enterprise?

數學基礎課2_歐拉函數,線性篩,擴歐

mapping索引属性 & 创建索的操作
随机推荐
Acwing第 59 场周赛(AK)
Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization
谷歌浏览器不能手动修改cookies,cookie报红标红
Guess The String (二分,交互)
第五章 我们可以直接调用run()方法来启动一个新线程吗
Decorate Apple Tree
Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends
有线电视网(树上分组)
MCU single chip OTP
[transfer] Darwin streaming server core code analysis
Typescript learning
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位
结合图片看常用串口通信UART
Markdown语法和常用快捷键
Interview review nth time
Introduction to basic knowledge of Minio
【力扣】复制带随机指针的链表
大龄程序员都去哪了?
Acwing game 58 (AK)