# 2015-08-13 [百度]--实习开发测试工程师--1面

1. 问简历

。。。

2. 算法

leetcode原题稍微改了下。

https://leetcode.com/problems/copy-list-with-random-pointer/

struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

/**
* 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:
// Time  Complexcity: O(n^2)
// Space Complexcity: O(n)
list<RandomListNode *> random_nodes;

return NULL;

} else {
}

RandomListNode *new_node;

while (old_node) {
new_node = new RandomListNode(old_node->label);
pre_node->next = new_node;
pre_node = new_node;

// Traverse random_nodes to find all the nodes whose random point to this node
list<RandomListNode *>::iterator it = random_nodes.begin();
while (it != random_nodes.end()) {
if (*it == old_node) {
*it = new_node;
random_nodes.erase(it);
} else
it++;
}

if (old_node->random == old_node) {
new_node->random = new_node;
} else {
new_node->random = old_node->random;
random_nodes.push_back(new_node->random);
}

old_node = old_node->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:
// Time  Complexcity: O(n)
// Space Complexcity: O(n)
unordered_map<RandomListNode *, list<RandomListNode *> > parent_nodes;

return NULL;

} else {
}

RandomListNode *new_node;

while (old_node) {
new_node = new RandomListNode(old_node->label);
pre_node->next = new_node;
pre_node = new_node;

// Update the random pointer
list<RandomListNode *> nodes = parent_nodes[old_node];
list<RandomListNode *>::iterator it = nodes.begin();
while (it != nodes.end()) {
it->random = new_node;
it++;
}

if (old_node->random == old_node) {
new_node->random = new_node;
} else {
parent_nodes[old_node->random].push_back(new_node);
}

old_node = old_node->next;
}
};

struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

/**
* 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:
// Time  Complexcity: O(n)
// Space Complexcity: O(1)
return NULL;

RandomListNode *next_old_node;

new_node->random = old_node;
next_old_node = old_node->next;
old_node->next = new_node;
old_node = next_old_node;

while (old_node) {
new_node = new RandomListNode(old_node->label);
pre_node->next = new_node;
pre_node = new_node;

new_node->random = old_node;
next_old_node = old_node->next;
old_node->next = new_node;
old_node = next_old_node;
}

while (new_node) {
old_node = new_node->random;
new_node->random = old_node->random == NULL ? NULL : (old_node->random)->next;
old_node->next = new_node->next == NULL ? NULL : (new_node->next)->random;

new_node = new_node->next;
}
};