-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyListWithRandomPointer.go
71 lines (64 loc) · 1.25 KB
/
copyListWithRandomPointer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build ignore
package main
import "fmt"
type Node struct {
Value int
Next *Node
Random *Node
}
func build(arr [][]int) *Node {
var nodes []*Node
for _, pair := range arr {
nodes = append(nodes, &Node{Value: pair[0]})
}
for i := 0; i < len(nodes)-1; i++ {
nodes[i].Next = nodes[i+1]
}
for i, pair := range arr {
if pair[1] != -1 {
nodes[i].Random = nodes[pair[1]]
}
}
return nodes[0]
}
func traverse(head *Node) {
for current := head; current != nil; current = current.Next {
randomValue := -1
if current.Random != nil {
randomValue = current.Random.Value
}
fmt.Printf("[%d, %d] -> ", current.Value, randomValue)
}
fmt.Println("nil")
}
func copyRandomList(head *Node) *Node {
mp := map[*Node]*Node{nil: nil}
cur := head
for cur != nil {
mp[cur] = &Node{Value: cur.Value}
cur = cur.Next
}
cur = head
for cur != nil {
copy := mp[cur]
copy.Next = mp[cur.Next]
copy.Random = mp[cur.Random]
cur = cur.Next
}
return mp[head]
}
func main() {
arr1 := [][]int{
{3, -1},
{7, 3},
{4, 0},
{5, 1},
}
head1 := build(arr1)
copiedHead1 := copyRandomList(head1)
traverse(copiedHead1)
arr2 := [][]int{{1, -1}, {2, 2}, {3, 2}}
head2 := build(arr2)
copiedHead2 := copyRandomList(head2)
traverse(copiedHead2)
}