代码拉取完成,页面将自动刷新
struct Node* copyRandomList(struct Node* head) {
struct Node* cur = head;
//在每个节点后面复制节点
while(cur)
{
struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
copy->val = cur->val;
copy->next = cur->next;
cur->next = copy;
cur = copy->next;
}
cur = head;
//复制random
while(cur)
{
struct Node* copy = cur->next;
if(cur->random == NULL)
{
copy->random = NULL;
}
else
{
copy->random = cur->random->next;
}
cur = copy->next;
}
//分离两个链表
cur = head;
struct Node* newHead = NULL;
struct Node* newTail = NULL;
while(cur)
{
struct Node* copy = cur->next;
if(newHead == NULL)
{
newHead = newTail = copy;
}
else{
newTail->next = copy;
newTail = newTail->next;
}
cur = copy->next;
}
return newHead;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。