增加LeetCode-138双百题解

This commit is contained in:
yirufeng
2021-01-20 08:20:39 +08:00
parent 5fa44b9f6d
commit 73764b7bcd
3 changed files with 1809 additions and 1547 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) {
}