Skip to content

138. Copy List with Random Pointer && upgrade to go1.23 #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.23'

- name: Build
run: go build -v ./...
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ result
leetcode.db
.vscode/

vendor/*
vendor/*
.DS_Store
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/TTWShell/algorithms

go 1.19
go 1.23

require github.com/stretchr/testify v1.7.0

Expand Down
48 changes: 48 additions & 0 deletions leetcode/linked-list/copyRandomList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* https://leetcode.com/problems/copy-list-with-random-pointer/?envType=study-plan-v2&envId=top-interview-150
*/

package lll

type Node struct {
Val int
Next *Node
Random *Node
}

func copyRandomList(head *Node) *Node {
if head == nil {
return nil
}

// Step 1: Insert copy nodes into original list
cur := head
for cur != nil {
newNode := &Node{Val: cur.Val, Next: cur.Next}
cur.Next = newNode
cur = newNode.Next
}

// Step 2: Assign random pointers for copied nodes
cur = head
for cur != nil {
if cur.Random != nil {
cur.Next.Random = cur.Random.Next
}
cur = cur.Next.Next
}

// Step 3: Separate original and copied lists
oldList := head
newList := head.Next
newHead := newList
for oldList != nil {
oldList.Next = oldList.Next.Next
if newList.Next != nil {
newList.Next = newList.Next.Next
}
oldList = oldList.Next
newList = newList.Next
}

return newHead
}
83 changes: 83 additions & 0 deletions leetcode/linked-list/copyRandomList_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package lll

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCopyRandomList(t *testing.T) {
// Helper function to build linked list from input
buildList := func(input [][]interface{}) *Node {
if len(input) == 0 {
return nil
}
nodes := make([]*Node, len(input))
for i, val := range input {
nodes[i] = &Node{Val: val[0].(int)}
}
for i, val := range input {
if i < len(input)-1 {
nodes[i].Next = nodes[i+1]
}
if val[1] != nil {
nodes[i].Random = nodes[val[1].(int)]
}
}
return nodes[0]
}

// Helper function to convert list to [][]interface{} for comparison
convertList := func(head *Node) [][]interface{} {
var result [][]interface{}
indexMap := make(map[*Node]int)
cur := head
index := 0
for cur != nil {
indexMap[cur] = index
cur = cur.Next
index++
}
cur = head
for cur != nil {
if cur.Random != nil {
result = append(result, []interface{}{cur.Val, indexMap[cur.Random]})
} else {
result = append(result, []interface{}{cur.Val, nil})
}

cur = cur.Next
}
return result
}

tests := []struct {
name string
input [][]interface{}
expected [][]interface{}
}{
{
name: "Example 1",
input: [][]interface{}{{7, nil}, {13, 0}, {11, 4}, {10, 2}, {1, 0}},
expected: [][]interface{}{{7, nil}, {13, 0}, {11, 4}, {10, 2}, {1, 0}},
},
{
name: "Example 2",
input: [][]interface{}{{1, 1}, {2, 1}},
expected: [][]interface{}{{1, 1}, {2, 1}},
},
{
name: "Example 3",
input: [][]interface{}{{3, nil}, {3, 0}, {3, nil}},
expected: [][]interface{}{{3, nil}, {3, 0}, {3, nil}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
head := buildList(tt.input)
copiedList := copyRandomList(head)
assert.Equal(t, tt.expected, convertList(copiedList))
})
}
}