From 300f49bd524ff2fcb198eb9e21bb505e650a3a6e Mon Sep 17 00:00:00 2001 From: "legolas.zhan" Date: Mon, 2 Dec 2024 16:13:23 +0800 Subject: [PATCH 1/2] 138. Copy List with Random Pointer && upgrade to go1.23 --- .gitignore | 3 +- go.mod | 2 +- leetcode/linked-list/copyRandomList.go | 48 ++++++++++++ leetcode/linked-list/copyRandomList_test.go | 83 +++++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 leetcode/linked-list/copyRandomList.go create mode 100644 leetcode/linked-list/copyRandomList_test.go diff --git a/.gitignore b/.gitignore index 9f765d7e..865c3f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ result leetcode.db .vscode/ -vendor/* \ No newline at end of file +vendor/* +.DS_Store diff --git a/go.mod b/go.mod index 09b460aa..e4f702f4 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/TTWShell/algorithms -go 1.19 +go 1.23 require github.com/stretchr/testify v1.7.0 diff --git a/leetcode/linked-list/copyRandomList.go b/leetcode/linked-list/copyRandomList.go new file mode 100644 index 00000000..baf8e90e --- /dev/null +++ b/leetcode/linked-list/copyRandomList.go @@ -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 +} diff --git a/leetcode/linked-list/copyRandomList_test.go b/leetcode/linked-list/copyRandomList_test.go new file mode 100644 index 00000000..9e398c72 --- /dev/null +++ b/leetcode/linked-list/copyRandomList_test.go @@ -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)) + }) + } +} From 4e782ee7680e33ce2ef47c4877f7d1132057df43 Mon Sep 17 00:00:00 2001 From: "legolas.zhan" Date: Mon, 2 Dec 2024 16:17:07 +0800 Subject: [PATCH 2/2] sync upgrade go version in ci --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 04403ea8..730c96a4 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 ./...