Skip to content

Commit

Permalink
这是easy题吧,如果只用map的话
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerxiong committed Jul 22, 2021
1 parent 5740e4c commit 6636115
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 138.复制带随机指针的链表.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @lc app=leetcode.cn id=138 lang=golang
*
* [138] 复制带随机指针的链表
*/

// @lc code=start
/**
* Definition for a Node.
* type Node struct {
* Val int
* Next *Node
* Random *Node
* }
*/

func copyRandomList(head *Node) *Node {
var m = make(map[*Node]*Node)
currNode := head
for currNode != nil {
m[currNode] = &Node{Val: currNode.Val, Next: currNode.Next, Random: currNode.Random}
currNode = currNode.Next
}
curr := m[head]
for curr != nil {
curr.Next = m[curr.Next]
curr.Random = m[curr.Random]
curr = curr.Next
}
return m[head]
}

// @lc code=end

0 comments on commit 6636115

Please sign in to comment.