Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1668cf

Browse files
添加题目67
1 parent b087f68 commit a1668cf

File tree

6 files changed

+190
-1
lines changed

6 files changed

+190
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @lc app=leetcode id=138 lang=cpp
3+
*
4+
* [138] Copy List with Random Pointer
5+
*/
6+
7+
// @lc code=start
8+
/*
9+
// Definition for a Node.
10+
class Node {
11+
public:
12+
int val;
13+
Node* next;
14+
Node* random;
15+
16+
Node(int _val) {
17+
val = _val;
18+
next = NULL;
19+
random = NULL;
20+
}
21+
};
22+
*/
23+
24+
class Solution {
25+
public:
26+
Node* copyRandomList(Node* head) {
27+
if (head == nullptr) return nullptr;
28+
Node* backup = new Node(0);
29+
Node* p = backup;
30+
unordered_map<Node*, int> m;
31+
vector<Node*> b;
32+
Node* h = head;
33+
for (int i = 0; h != nullptr; i++)
34+
{
35+
p->next = new Node(h->val);
36+
b.push_back(p->next);
37+
pair<Node*, int> a;
38+
a.first = h;
39+
a.second = i;
40+
m.insert(a);
41+
p = p->next;
42+
h = h->next;
43+
}
44+
p = backup->next;
45+
h = head;
46+
for (int i = 0; h != nullptr; i++)
47+
{
48+
if (h->random != nullptr)
49+
{
50+
int which = m[h->random];
51+
p->random = b[which];
52+
}
53+
p = p->next;
54+
h = h->next;
55+
}
56+
return backup->next;
57+
}
58+
};
59+
// @lc code=end
60+
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# 复制带随机指针的链表
2+
3+
#### *Copy List with Random Pointer*
4+
5+
给你一个长度为 `n` 的链表,每个节点包含一个额外增加的随机指针 `random` ,该指针可以指向链表中的任何节点或空节点。
6+
7+
构造这个链表的 **深拷贝**。 深拷贝应该正好由 `n`**全新** 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 `next` 指针和 `random` 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。**复制链表中的指针都不应指向原链表中的节点**
8+
9+
例如,如果原链表中有 `X``Y `两个节点,其中 `X.random --> Y` 。那么在复制链表中对应的两个节点 `x``y` ,同样有 `x.random --> y`
10+
11+
返回复制链表的头节点。
12+
13+
用一个由 `n` 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 `[val, random_index]` 表示:
14+
15+
* `val`:一个表示 `Node.val` 的整数。
16+
* `random_index`:随机指针指向的节点索引(范围从 `0``n-1`);如果不指向任何节点,则为 `null`
17+
你的代码 **** 接受原链表的头节点 `head` 作为传入参数。
18+
19+
20+
21+
22+
英文题目:
23+
24+
A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
25+
26+
Construct a **deep copy** of the list. The deep copy should consist of exactly `n` **brand new** nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. **None of the pointers in the new list should point to nodes in the original list**.
27+
28+
For example, if there are two nodes X and Y in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`.
29+
30+
Return the head of the copied linked list.
31+
32+
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of `[val, random_index]` where:
33+
34+
* `val`: an integer representing `Node.val`
35+
* `random_index`: the index of the node (range from `0` to `n-1`) that the random pointer points to, or `null` if it does not point to any node.
36+
Your code will **only** be given the head of the original linked list.
37+
38+
39+
40+
**Constraints:**
41+
42+
- The number of nodes in the list is in the range `[0, 200]`.
43+
- `-100 <= Node.val <= 100`
44+
- `-200 <= x <= 200`
45+
46+
**example 1**
47+
48+
![example 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/67.%20Copy%20List%20with%20Random%20Pointer(Medium)/e1.png)
49+
50+
```
51+
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
52+
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
53+
```
54+
55+
**example 2**
56+
57+
![example 2](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/67.%20Copy%20List%20with%20Random%20Pointer(Medium)/e1.png)
58+
59+
```
60+
Input: head = [[1,1],[2,1]]
61+
Output: [[1,1],[2,1]]
62+
```
63+
64+
**example 3**
65+
66+
![example 3](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/67.%20Copy%20List%20with%20Random%20Pointer(Medium)/e3.png)
67+
68+
```
69+
Input: head = [[3,null],[3,0],[3,null]]
70+
Output: [[3,null],[3,0],[3,null]]
71+
```
72+
73+
74+
75+
---
76+
77+
### 思路
78+
79+
1. 使用`map`存储原链表中每个节点`地址`(key)和其在表中的`序号`(value)
80+
2. 先遍历原链表,复制每个节点`val``next`,将新节点存入数组`b`
81+
3. 再次遍历原链表,可从`map`中查询到该节点的`random`节点的序号`i`,`b[i]`即为对应新节点的`random`需要指向的位置
82+
4. 注意处理`null`
83+
84+
85+
### 代码
86+
```cpp
87+
88+
class Solution {
89+
public:
90+
Node* copyRandomList(Node* head) {
91+
if (head == nullptr) return nullptr;
92+
Node* backup = new Node(0);
93+
Node* p = backup;
94+
unordered_map<Node*, int> m;
95+
vector<Node*> b;
96+
Node* h = head;
97+
for (int i = 0; h != nullptr; i++)
98+
{
99+
p->next = new Node(h->val);
100+
b.push_back(p->next);
101+
pair<Node*, int> a;
102+
a.first = h;
103+
a.second = i;
104+
m.insert(a);
105+
p = p->next;
106+
h = h->next;
107+
}
108+
p = backup->next;
109+
h = head;
110+
for (int i = 0; h != nullptr; i++)
111+
{
112+
if (h->random != nullptr)
113+
{
114+
int which = m[h->random];
115+
p->random = b[which];
116+
}
117+
p = p->next;
118+
h = h->next;
119+
}
120+
return backup->next;
121+
}
122+
};
123+
124+
// @lc code=end
125+
```
126+
127+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
61 KB
Loading[フレーム]
35.5 KB
Loading[フレーム]
47.5 KB
Loading[フレーム]

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@
6666

6767
##### 65. [83.Remove Duplicates from Sorted List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/65.%20Remove%20Duplicates%20from%20Sorted%20List(Easy)) 删除排序链表中的重复元素
6868

69-
##### 65. [86.Partition List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/66.%20Partition%20List(Medium)) 分隔链表
69+
##### 66. [86.Partition List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/66.%20Partition%20List(Medium)) 分隔链表
70+
71+
##### 67. [138.Copy List with Random Pointer](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/67.%20Copy%20List%20with%20Random%20Pointer(Medium)) 复制带随机指针的链表

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /