mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
Merge branch 'master' of github.com:halfrost/LeetCode-Go
This commit is contained in:
@ -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
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package leetcode
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_Problem138(t *testing.T) {
|
||||
|
||||
}
|
Reference in New Issue
Block a user