Skip to content

Commit e9b9d6d

Browse files
committed
138修正
1 parent 0d83d12 commit e9b9d6d

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

leetcode-138-Copy-List-with-Random-Pointer.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
这里的话就有两种思路,一种需要遍历两边链表,一种只需要遍历一遍。
1414

15+
> 2020.3.3 更新,leetcode 增加了样例,之前没有重复的数字所以 `key` 存的 `val` ,现在有了重复数字,将 `key` 修改为 `Node`。此外 `Node` 的无参的构造函数也被去掉了,也需要修改。
16+
1517
# 解法一
1618

1719
首先利用 `HashMap` 来一个不用思考的代码。
@@ -25,27 +27,24 @@ public Node copyRandomList(Node head) {
2527
if (head == null) {
2628
return null;
2729
}
28-
HashMap<Integer, Node> map = new HashMap<>();
30+
HashMap<Node, Node> map = new HashMap<>();
2931
Node h = head;
30-
//生成所有节点
3132
while (h != null) {
32-
Node t = new Node();
33-
t.val = h.val;
34-
map.put(t.val, t);
33+
Node t = new Node(h.val);
34+
map.put(h, t);
3535
h = h.next;
3636
}
3737
h = head;
38-
//更新 next 和 random
3938
while (h != null) {
4039
if (h.next != null) {
41-
map.get(h.val).next = map.get(h.next.val);
40+
map.get(h).next = map.get(h.next);
4241
}
4342
if (h.random != null) {
44-
map.get(h.val).random = map.get(h.random.val);
43+
map.get(h).random = map.get(h.random);
4544
}
4645
h = h.next;
4746
}
48-
return map.get(head.val);
47+
return map.get(head);
4948
}
5049
```
5150

@@ -80,26 +79,24 @@ public Node copyRandomList(Node head) {
8079
if (head == null) {
8180
return null;
8281
}
83-
HashMap<Integer, Node> map = new HashMap<>();
82+
HashMap<Node, Node> map = new HashMap<>();
8483
Node h = head;
85-
Node cur = new Node(); //空结点,dummy 节点,为了方便头结点计算
84+
Node cur = new Node(-1); //空结点,dummy 节点,为了方便头结点计算
8685
while (h != null) {
8786
//判断当前节点是否已经产生过
88-
if (!map.containsKey(h.val)) {
89-
Node t = new Node();
90-
t.val = h.val;
91-
map.put(t.val, t);
87+
if (!map.containsKey(h)) {
88+
Node t = new Node(h.val);
89+
map.put(h, t);
9290
}
9391
//得到当前节点去更新它的 random 指针
94-
Node next = map.get(h.val);
92+
Node next = map.get(h);
9593
if (h.random != null) {
9694
//判断当前节点是否已经产生过
97-
if (!map.containsKey(h.random.val)) {
98-
next.random = new Node();
99-
next.random.val = h.random.val;
100-
map.put(next.random.val, next.random);
95+
if (!map.containsKey(h.random)) {
96+
next.random = new Node(h.random.val);
97+
map.put(h.random, next.random);
10198
} else {
102-
next.random = map.get(h.random.val);
99+
next.random = map.get(h.random);
103100
}
104101

105102
}
@@ -108,7 +105,7 @@ public Node copyRandomList(Node head) {
108105
cur = cur.next;
109106
h = h.next;
110107
}
111-
return map.get(head.val);
108+
return map.get(head);
112109
}
113110
```
114111

@@ -139,8 +136,7 @@ public Node copyRandomList(Node head) {
139136
Node l2 = null;
140137
//生成所有的节点,并且分别插入到原有节点的后边
141138
while (l1 != null) {
142-
l2 = new Node();
143-
l2.val = l1.val;
139+
l2 = new Node(l1.val);
144140
l2.next = l1.next;
145141
l1.next = l2;
146142
l1 = l1.next.next;
@@ -197,8 +193,7 @@ public Node copyRandomList(Node head) {
197193
//生成所有的节点,讲它们保存到原链表的 random 域,
198194
//同时利用新生成的节点的 next 域保存原链表的 random。
199195
while (l1 != null) {
200-
l2 = new Node();
201-
l2.val = l1.val;
196+
l2 = new Node(l1.val);
202197
l2.next = l1.random;
203198
l1.random = l2;
204199
l1 = l1.next;
@@ -226,4 +221,5 @@ public Node copyRandomList(Node head) {
226221

227222
#
228223

229-
解法一、解法二是比较直接的想法,直接利用 `HashMap` 存储之前的节点。解法三、解法四利用原有链表的指针,通过指来指去完成了赋值。链表操作的核心思想就是,在改变某一个节点的指针域的时候,一定要把该节点的指针指向的节点用另一个指针保存起来,以免造成丢失。
224+
解法一、解法二是比较直接的想法,直接利用 `HashMap` 存储之前的节点。解法三、解法四利用原有链表的指针,通过指来指去完成了赋值。链表操作的核心思想就是,在改变某一个节点的指针域的时候,一定要把该节点的指针指向的节点用另一个指针保存起来,以免造成丢失。
225+

0 commit comments

Comments
 (0)