Skip to content

Commit

Permalink
Time: 3 ms (57.3%), Space: 5.5 MB (7.12%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Dec 26, 2024
1 parent 928827d commit 6c37136
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a Node.
* type Node struct {
* Val int
* Next *Node
* Random *Node
* }
*/

func copyRandomList(head *Node) *Node {
oldToCopy := map[*Node]*Node{}
cur := head

for cur != nil {
copy := &Node{Val: cur.Val}
oldToCopy[cur] = copy
cur = cur.Next
}

cur = head
for cur != nil {
copy := oldToCopy[cur]
copy.Next = oldToCopy[cur.Next]
copy.Random = oldToCopy[cur.Random]
cur = cur.Next
}

return oldToCopy[head]
}

0 comments on commit 6c37136

Please sign in to comment.