创新工场笔试题Copy List with Random Pointer

假设有如下一个链表:


1

2

3

4

5

6

struct
Node

{

    int

value ;

    struct
Node *next ;

    struct
Node *random ;

}

其中,random指向该链表的任意一个节点或者NULL,请编程实现该链表的深拷贝。


1

Node
*deepCopy (Node *head)

解析:把新节点插入到相应的旧节点后面:

分两步

1、构建新节点random指针:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->next

2、恢复原始链表以及构建新链表:例如old1->next = old1->next->next, new1->next = new1->next->next

该算法时间复杂度O(N),空间复杂度O(1)

代码:

Node *deepCopy (Node *head)
{
    Node* now = head;
    Node* next = head->next;
    while( now != NULL )
    {
         Node * copy = new Node;
         copy->value = now->value;
         copy->next  = now->next;
         now ->next  = copy;
         now         = next;
         next        = next->next;
    }
    now = head;
    while( now != NULL )
    {
         now->next->random = now->random->next;
         now = now->next->next;
    }
    Node* head2 = head->next;
    Node* newHead = head->next;
    while( head2->next != NULL )
    {
        head->next = head2->next;
        head2->next = head2->next->next;
        head = head->next;
        head2 = head2->next;
    }

    return newHead;
}
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 01:02:05

创新工场笔试题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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

2013 创新工场笔试题

1.b. 结构体中的位域对齐.前两个int型变量的位域之和小于4字节,因此存放在一个int型变量之中,但是第三个是double型的变量,需要8字节对齐,这样前三个变量占了16个字节,最后一个int是四个字节,总共20个字节.根据结构体定义的原则,应该是最长变量的整数倍对齐,因此应该是24个字节. 对于window而言,结构体的最大类型是多少字节,就是多少字节对齐,但是对已linux而言,最长是4字节对齐. 2.b 3.网络地址是:11111111.11111111.11111111.111000

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 random pointer. * struct RandomListNode

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. 解题思路:这题主要是需要深

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

133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

[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

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label a