Skip to content

Commit 7325f78

Browse files
committed
Copy list with random pointer
1 parent d4306e9 commit 7325f78

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// 138. Copy list with random pointer
2+
// Topics: 'Hash Table', 'Linked List'
3+
// Level: 'Medium'
4+
5+
// 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.
6+
7+
// 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.
8+
9+
// 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.
10+
11+
// Return the head of the copied linked list.
12+
13+
// 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:
14+
15+
// val: an integer representing Node.val
16+
// 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.
17+
18+
// Your code will only be given the head of the original linked list.
19+
20+
// Example 1:
21+
22+
// Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
23+
// Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
24+
25+
// Example 2:
26+
27+
// Input: head = [[1,1],[2,1]]
28+
// Output: [[1,1],[2,1]]
29+
30+
// Example 3:
31+
32+
// Input: head = [[3,null],[3,0],[3,null]]
33+
// Output: [[3,null],[3,0],[3,null]]
34+
35+
// Constraints:
36+
37+
// 0 <= n <= 1000
38+
// -104 <= Node.val <= 104
39+
// Node.random is null or is pointing to some node in the linked list.
40+
41+
package copylistwithrandompointer
42+
43+
func copyRandomList(head *Node) *Node {
44+
if head == nil {
45+
return head
46+
}
47+
m := make(map[*Node]*Node)
48+
for cur := head; cur != nil; cur = cur.Next {
49+
m[cur] = &Node{Val: cur.Val}
50+
}
51+
52+
for cur := head; cur != nil; cur = cur.Next {
53+
copied := m[cur]
54+
copied.Next = m[cur.Next]
55+
copied.Random = m[cur.Random]
56+
}
57+
58+
return m[head]
59+
}
60+
61+
type Node struct {
62+
Random *Node
63+
Next *Node
64+
Val int
65+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package copylistwithrandompointer
2+
3+
import "testing"
4+
5+
func TestCopyRandomList(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
input [][]interface{}
9+
}{
10+
{
11+
name: "example 1",
12+
input: [][]interface{}{{7, nil}, {13, 0}, {11, 4}, {10, 2}, {1, 0}},
13+
},
14+
{
15+
name: "example 2",
16+
input: [][]interface{}{{1, 1}, {2, 1}},
17+
},
18+
{
19+
name: "example 3",
20+
input: [][]interface{}{{3, nil}, {3, 0}, {3, nil}},
21+
},
22+
{
23+
name: "empty list",
24+
input: [][]interface{}{},
25+
},
26+
{
27+
name: "single node no random",
28+
input: [][]interface{}{{1, nil}},
29+
},
30+
{
31+
name: "single node self random",
32+
input: [][]interface{}{{1, 0}},
33+
},
34+
{
35+
name: "all random null",
36+
input: [][]interface{}{{1, nil}, {2, nil}, {3, nil}},
37+
},
38+
{
39+
name: "all random to first",
40+
input: [][]interface{}{{1, nil}, {2, 0}, {3, 0}, {4, 0}},
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
head := buildList(tt.input)
47+
copied := copyRandomList(head)
48+
49+
if !verifyDeepCopy(head, copied, tt.input) {
50+
t.Errorf("Deep copy verification failed")
51+
}
52+
})
53+
}
54+
}
55+
56+
func buildList(data [][]interface{}) *Node {
57+
if len(data) == 0 {
58+
return nil
59+
}
60+
61+
nodes := make([]*Node, len(data))
62+
for i, pair := range data {
63+
nodes[i] = &Node{Val: pair[0].(int)}
64+
}
65+
66+
for i := 0; i < len(nodes)-1; i++ {
67+
nodes[i].Next = nodes[i+1]
68+
}
69+
70+
for i, pair := range data {
71+
if pair[1] != nil {
72+
idx := pair[1].(int)
73+
nodes[i].Random = nodes[idx]
74+
}
75+
}
76+
77+
return nodes[0]
78+
}
79+
80+
func verifyDeepCopy(original, copied *Node, data [][]interface{}) bool {
81+
if original == nil && copied == nil {
82+
return true
83+
}
84+
if original == nil || copied == nil {
85+
return false
86+
}
87+
88+
origNodes := []*Node{}
89+
copyNodes := []*Node{}
90+
91+
for cur := original; cur != nil; cur = cur.Next {
92+
origNodes = append(origNodes, cur)
93+
}
94+
95+
for cur := copied; cur != nil; cur = cur.Next {
96+
copyNodes = append(copyNodes, cur)
97+
}
98+
99+
if len(origNodes) != len(copyNodes) {
100+
return false
101+
}
102+
103+
for i := 0; i < len(origNodes); i++ {
104+
if origNodes[i] == copyNodes[i] {
105+
return false
106+
}
107+
108+
if origNodes[i].Val != copyNodes[i].Val {
109+
return false
110+
}
111+
}
112+
113+
for i := 0; i < len(origNodes); i++ {
114+
origRandom := origNodes[i].Random
115+
copyRandom := copyNodes[i].Random
116+
117+
if origRandom == nil && copyRandom == nil {
118+
continue
119+
}
120+
if origRandom == nil || copyRandom == nil {
121+
return false
122+
}
123+
124+
origIdx := -1
125+
for j, node := range origNodes {
126+
if node == origRandom {
127+
origIdx = j
128+
break
129+
}
130+
}
131+
132+
if origIdx == -1 {
133+
return false
134+
}
135+
136+
if copyRandom != copyNodes[origIdx] {
137+
return false
138+
}
139+
140+
if copyRandom == origRandom {
141+
return false
142+
}
143+
}
144+
145+
return true
146+
}

0 commit comments

Comments
 (0)