Merge branch 'master' of github.com:halfrost/LeetCode-Go

This commit is contained in:
YDZ
2021-01-21 15:53:34 +08:00
3 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,46 @@
package leetcode
//执行用时0 ms, 在所有 Go 提交中击败了100.00%的用户
//内存消耗3.4 MB, 在所有 Go 提交中击败了100.00%的用户
type Node struct {
Val int
Next *Node
Random *Node
}
func copyRandomList(head *Node) *Node {
if head == nil {
return nil
}
tempHead := copyNodeToLinkedList(head)
return splitLinkedList(tempHead)
}
func splitLinkedList(head *Node) *Node {
cur := head
head = head.Next
for cur != nil && cur.Next != nil {
cur.Next, cur = cur.Next.Next, cur.Next
}
return head
}
func copyNodeToLinkedList(head *Node) *Node {
cur := head
for cur != nil {
node := &Node{
Val: cur.Val,
Next: cur.Next,
}
cur.Next, cur = node, cur.Next
}
cur = head
for cur != nil {
if cur.Random != nil {
cur.Next.Random = cur.Random.Next
}
cur = cur.Next.Next
}
return head
}

View File

@ -0,0 +1,7 @@
package leetcode
import "testing"
func Test_Problem138(t *testing.T) {
}