Skip to content

Commit a6a14a0

Browse files
author
edboffical
committed
add 138
1 parent 8641132 commit a6a14a0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

copy-list-with-random-pointer.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
// 138 https://leetcode-cn.com/problems/copy-list-with-random-pointer/
4+
// 哈希 哈希的 key 是旧链表,有指向关系 哈希的 value 是新链表 只是结构
5+
func copyRandomList(head *Node) *Node {
6+
h := map[*Node]*Node{}
7+
for p := head; p != nil; p = p.Next {
8+
h[p] = &Node{p.Val, nil, nil}
9+
}
10+
for p := head; p != nil; p = p.Next {
11+
h[p].Next = h[p.Next]
12+
h[p].Random = h[p.Random]
13+
}
14+
return h[head]
15+
}

0 commit comments

Comments
 (0)