From c0e8b75bfd323b9dd304301b28969dc7d9d6cb75 Mon Sep 17 00:00:00 2001 From: Cathay Date: Thu, 29 Dec 2022 13:26:09 +0800 Subject: [PATCH 1/5] feat(array): add the Go code to array docs (Chapter of Array and LinkedList) --- .../go/chapter_array_and_linkedlist/array.go | 78 +++++++++++++++++++ .../array_test.go | 28 +++++++ docs/chapter_array_and_linkedlist/array.md | 63 +++++++++++++-- 3 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 codes/go/chapter_array_and_linkedlist/array.go create mode 100644 codes/go/chapter_array_and_linkedlist/array_test.go diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go new file mode 100644 index 000000000..416957ff3 --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -0,0 +1,78 @@ +// File: array.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "math/rand" +) + +/* 初始化数组 */ +var arr = [5]int{} +var nums = []int{1, 3, 2, 5, 4} + +/* 随机返回一个数组元素 */ +func randomAccess(nums []int) int { + randomIndex := rand.Intn(len(nums)) + randomNum := nums[randomIndex] + return randomNum +} + +/* 扩展数组长度 */ +func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i, num := range nums { + res[i] = num + } + // 返回扩展后的新数组 + return res +} + +/* 在数组的索引 index 处插入元素 num */ +func insert(nums []int, size int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + for i := size - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num +} + +/* 删除索引 index 处元素 */ +func remove(nums []int, size int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < size-1; i++ { + nums[i] = nums[i+1] + } +} + +/* 遍历数组 */ +func traverse(nums []int) { + count := 0 + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + fmt.Println(count) + + count = 0 + // 直接遍历数组 + for range nums { + count++ + } + fmt.Println(count) +} + +/* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ +func find(nums []int, target int) int { + for i := 0; i < len(nums); i++ { + if nums[i] == target { + return i + } + } + return -1 +} diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go new file mode 100644 index 000000000..37f75a55d --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -0,0 +1,28 @@ +// File: array_test.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "testing" +) + +func TestArray(t *testing.T) { + nums := make([]int, 5) + fmt.Println("randomAccess:", randomAccess(nums)) + + fmt.Println("extend:", extend(nums, 5)) + + insert(nums, 5, 2, 2) + fmt.Println("after insert:", nums) + + remove(nums, 5, 2) + fmt.Println("after remove:", nums) + + fmt.Println("traverse nums:") + traverse(nums) + + fmt.Println("find value 2 key:", find(nums, 2)) +} diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 70e30129a..bae5c2772 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -43,7 +43,8 @@ comments: true === "Go" ```go title="array.go" - + var arr = [5]int{} + var nums = [5]int{1, 3, 2, 5, 4} ``` === "JavaScript" @@ -136,7 +137,12 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 随机返回一个数组元素 */ + func randomAccess(nums []int) int { + randomIndex := rand.Intn(len(nums)) + randomNum := nums[randomIndex] + return randomNum + } ``` === "JavaScript" @@ -239,7 +245,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 扩展数组长度 */ + func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i, num := range nums { + res[i] = num + } + // 返回扩展后的新数组 + return res + } ``` === "JavaScript" @@ -373,7 +389,23 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组的索引 index 处插入元素 num */ + func insert(nums []int, size int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + for i := size - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num + } + + /* 删除索引 index 处元素 */ + func remove(nums []int, size int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < size-1; i++ { + nums[i] = nums[i+1] + } + } ``` === "JavaScript" @@ -502,7 +534,18 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 遍历数组 */ + func traverse(nums []int) { + count := 0 + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + // 直接遍历数组 + for range nums { + count++ + } + } ``` === "JavaScript" @@ -607,7 +650,15 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ + func find(nums []int, target int) int { + for i := 0; i < len(nums); i++ { + if nums[i] == target { + return i + } + } + return -1 + } ``` === "JavaScript" From 1062fe64a398aacb8c4e00e17c03e57289dd9ae9 Mon Sep 17 00:00:00 2001 From: Cathay Date: Thu, 29 Dec 2022 17:43:16 +0800 Subject: [PATCH 2/5] feat(linked_list): add the Go code to linked_list docs (Chapter of Array and LinkedList) --- .../linked_list.go | 105 ++++++++++++++++++ .../linked_list_test.go | 47 ++++++++ .../linked_list.md | 82 +++++++++++++- 3 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 codes/go/chapter_array_and_linkedlist/linked_list.go create mode 100644 codes/go/chapter_array_and_linkedlist/linked_list_test.go diff --git a/codes/go/chapter_array_and_linkedlist/linked_list.go b/codes/go/chapter_array_and_linkedlist/linked_list.go new file mode 100644 index 000000000..3129a0eaf --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -0,0 +1,105 @@ +// File: linked_list.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "strconv" + "strings" +) + +/* 链表结点结构体 */ +type ListNode struct { + Val int // 结点值 + Next *ListNode // 指向下一结点的指针(引用) +} + +// NewListNode 构造函数,创建一个新的链表 +func NewListNode(val int) *ListNode { + return &ListNode{ + Val: val, + Next: nil, + } +} + +// PrintLinkedList Print a linked list +func PrintLinkedList(node *ListNode) { + if node == nil { + return + } + var builder strings.Builder + for node.Next != nil { + builder.WriteString(strconv.Itoa(node.Val) + " -> ") + node = node.Next + } + builder.WriteString(strconv.Itoa(node.Val)) + fmt.Println(builder.String()) +} + +/* 在链表的结点 n0 之后插入结点 P */ +func insertNode(n0 *ListNode, P *ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 +} + +/* 删除链表的结点 n0 之后的首个结点 */ +func removeNode(n0 *ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 +} + +/* 访问链表中索引为 index 的结点 */ +func access(head *ListNode, index int) *ListNode { + for i := 0; i < index; i++ { + head = head.Next + if head == nil { + return nil + } + } + return head +} + +/* 在链表中查找值为 target 的首个结点 */ +func findNode(head *ListNode, target int) int { + index := 0 + for head != nil { + if head.Val == target { + return index + } + head = head.Next + index++ + } + return -1 +} + +/* 双向链表结点结构体 */ +type DoublyListNode struct { + Val int // 结点值 + Next *DoublyListNode // 指向后继结点的指针(引用) + Prev *DoublyListNode // 指向前驱结点的指针(引用) +} + +// NewDoublyListNode 初始化 +func NewDoublyListNode(val int, nodes ...*DoublyListNode) *DoublyListNode { + var next, prev *DoublyListNode + length := len(nodes) + if length > 0 { + next = nodes[0] + } + if length > 1 { + prev = nodes[1] + } + return &DoublyListNode{ + Val: val, + Next: next, + Prev: prev, + } +} diff --git a/codes/go/chapter_array_and_linkedlist/linked_list_test.go b/codes/go/chapter_array_and_linkedlist/linked_list_test.go new file mode 100644 index 000000000..5deabff88 --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/linked_list_test.go @@ -0,0 +1,47 @@ +// File: linked_list_test.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "testing" +) + +func TestLikedList(t *testing.T) { + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := NewListNode(1) + n1 := NewListNode(3) + n2 := NewListNode(2) + n3 := NewListNode(5) + n4 := NewListNode(4) + + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 + + fmt.Println("链表结构:") + PrintLinkedList(n0) + + P := NewListNode(6) + + /* 在链表的结点 n0 之后插入结点 P */ + insertNode(n0, P) + fmt.Println("在链表的结点 n0 之后插入结点 P 后,链表结构:") + PrintLinkedList(n0) + + /* 删除链表的结点 n0 之后的首个结点 */ + removeNode(n0) + fmt.Println("删除链表的结点 n0 之后的首个结点后,链表结构:") + PrintLinkedList(n0) + + /* 访问链表中索引为 index 的结点 */ + fmt.Println("访问链表中索引为 2 的结点:", access(n0, 2)) + + /* 在链表中查找值为 target 的首个结点 */ + fmt.Println("在链表中查找 5 的首个节点索引值:", findNode(n0, 5)) +} diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 6ad1d5658..83c8db64e 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -51,7 +51,19 @@ comments: true === "Go" ```go title="" - + /* 链表结点结构体 */ + type ListNode struct { + Val int // 结点值 + Next *ListNode // 指向下一结点的指针(引用) + } + + // NewListNode 构造函数,创建一个新的链表 + func NewListNode(val int) *ListNode { + return &ListNode{ + Val: val, + Next: nil, + } + } ``` === "JavaScript" @@ -162,7 +174,19 @@ comments: true === "Go" ```go title="" + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := NewListNode(1) + n1 := NewListNode(3) + n2 := NewListNode(2) + n3 := NewListNode(5) + n4 := NewListNode(4) + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 ``` === "JavaScript" @@ -294,7 +318,23 @@ comments: true === "Go" ```go title="" + /* 在链表的结点 n0 之后插入结点 P */ + func insert(n0 *ListNode, P *ListNode) { + n1 := n0.Next + n0.Next = P + P.Next = n1 + } + /* 删除链表的结点 n0 之后的首个结点 */ + func removeNode(n0 *ListNode) { + if n0.Next == nil { + return + } + // n0 -> P -> n1 + P := n0.Next + n1 := P.Next + n0.Next = n1 + } ``` === "JavaScript" @@ -415,7 +455,16 @@ comments: true === "Go" ```go title="" - + /* 访问链表中索引为 index 的结点 */ + func access(head *ListNode, index int) *ListNode { + for i := 0; i < index; i++ { + head = head.Next + if head == nil { + return nil + } + } + return head + } ``` === "JavaScript" @@ -524,7 +573,18 @@ comments: true === "Go" ```go title="" - + /* 在链表中查找值为 target 的首个结点 */ + func find(head *ListNode, target int) int { + index := 0 + for head != nil { + if head.Val == target { + return index + } + head = head.Next + index++ + } + return -1 + } ``` === "JavaScript" @@ -631,7 +691,21 @@ comments: true === "Go" ```go title="" - + /* 双向链表结点结构体 */ + type DoublyListNode struct { + Val int // 结点值 + Next *DoublyListNode // 指向后继结点的指针(引用) + Prev *DoublyListNode // 指向前驱结点的指针(引用) + } + + // NewDoublyListNode 初始化 + func NewDoublyListNode(val int) *DoublyListNode { + return &DoublyListNode{ + Val: val, + Next: nil, + Prev: nil, + } + } ``` === "JavaScript" From cd9f4fc35d48cbb0547622d40fcef2d01e814d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E5=A4=AA?= Date: Thu, 29 Dec 2022 21:49:46 +0800 Subject: [PATCH 3/5] fix(array and linkedlist): fix that the printing in the test function is the same as that in other languages --- .../go/chapter_array_and_linkedlist/array.go | 12 ++-- .../array_test.go | 33 +++++++--- .../linked_list.go | 64 ++----------------- .../linked_list_test.go | 42 ++++++------ 4 files changed, 54 insertions(+), 97 deletions(-) diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go index 416957ff3..1a02bea47 100644 --- a/codes/go/chapter_array_and_linkedlist/array.go +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -9,10 +9,6 @@ import ( "math/rand" ) -/* 初始化数组 */ -var arr = [5]int{} -var nums = []int{1, 3, 2, 5, 4} - /* 随机返回一个数组元素 */ func randomAccess(nums []int) int { randomIndex := rand.Intn(len(nums)) @@ -33,9 +29,9 @@ func extend(nums []int, enlarge int) []int { } /* 在数组的索引 index 处插入元素 num */ -func insert(nums []int, size int, num int, index int) { +func insert(nums []int, num int, index int) { // 把索引 index 以及之后的所有元素向后移动一位 - for i := size - 1; i > index; i-- { + for i := len(nums) - 1; i > index; i-- { nums[i] = nums[i-1] } // 将 num 赋给 index 处元素 @@ -43,9 +39,9 @@ func insert(nums []int, size int, num int, index int) { } /* 删除索引 index 处元素 */ -func remove(nums []int, size int, index int) { +func remove(nums []int, index int) { // 把索引 index 之后的所有元素向前移动一位 - for i := index; i < size-1; i++ { + for i := index; i < len(nums)-1; i++ { nums[i] = nums[i+1] } } diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go index 37f75a55d..4938ac509 100644 --- a/codes/go/chapter_array_and_linkedlist/array_test.go +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -9,20 +9,35 @@ import ( "testing" ) +/* Driver Code */ func TestArray(t *testing.T) { - nums := make([]int, 5) - fmt.Println("randomAccess:", randomAccess(nums)) + /* 初始化数组 */ + var arr = [5]int{} + fmt.Println("数组 arr =", arr) - fmt.Println("extend:", extend(nums, 5)) + var nums = []int{1, 3, 2, 5, 4} + fmt.Println("数组 nums =", nums) - insert(nums, 5, 2, 2) - fmt.Println("after insert:", nums) + /* 随机访问 */ + randomNum := randomAccess(nums) + fmt.Println("在 nums 中获取随机元素", randomNum) - remove(nums, 5, 2) - fmt.Println("after remove:", nums) + /* 长度扩展 */ + nums = extend(nums, 3) + fmt.Println("将数组长度扩展至 8 ,得到 nums =", nums) - fmt.Println("traverse nums:") + /* 插入元素 */ + insert(nums, 6, 3) + fmt.Println("在索引 3 处插入数字 6 ,得到 nums =", nums) + + /* 删除元素 */ + remove(nums, 2) + fmt.Println("删除索引 2 处的元素,得到 nums = ", nums) + + /* 遍历数组 */ traverse(nums) - fmt.Println("find value 2 key:", find(nums, 2)) + /* 查找元素 */ + index := find(nums, 3) + fmt.Println("在 nums 中查找元素 3 ,得到索引 =", index) } diff --git a/codes/go/chapter_array_and_linkedlist/linked_list.go b/codes/go/chapter_array_and_linkedlist/linked_list.go index 3129a0eaf..e7fdb35ca 100644 --- a/codes/go/chapter_array_and_linkedlist/linked_list.go +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -5,48 +5,18 @@ package chapter_array_and_linkedlist import ( - "fmt" - "strconv" - "strings" + "github.com/krahets/hello-algo/pkg" ) -/* 链表结点结构体 */ -type ListNode struct { - Val int // 结点值 - Next *ListNode // 指向下一结点的指针(引用) -} - -// NewListNode 构造函数,创建一个新的链表 -func NewListNode(val int) *ListNode { - return &ListNode{ - Val: val, - Next: nil, - } -} - -// PrintLinkedList Print a linked list -func PrintLinkedList(node *ListNode) { - if node == nil { - return - } - var builder strings.Builder - for node.Next != nil { - builder.WriteString(strconv.Itoa(node.Val) + " -> ") - node = node.Next - } - builder.WriteString(strconv.Itoa(node.Val)) - fmt.Println(builder.String()) -} - /* 在链表的结点 n0 之后插入结点 P */ -func insertNode(n0 *ListNode, P *ListNode) { +func insertNode(n0 *pkg.ListNode, P *pkg.ListNode) { n1 := n0.Next n0.Next = P P.Next = n1 } /* 删除链表的结点 n0 之后的首个结点 */ -func removeNode(n0 *ListNode) { +func removeNode(n0 *pkg.ListNode) { if n0.Next == nil { return } @@ -57,7 +27,7 @@ func removeNode(n0 *ListNode) { } /* 访问链表中索引为 index 的结点 */ -func access(head *ListNode, index int) *ListNode { +func access(head *pkg.ListNode, index int) *pkg.ListNode { for i := 0; i < index; i++ { head = head.Next if head == nil { @@ -68,7 +38,7 @@ func access(head *ListNode, index int) *ListNode { } /* 在链表中查找值为 target 的首个结点 */ -func findNode(head *ListNode, target int) int { +func findNode(head *pkg.ListNode, target int) int { index := 0 for head != nil { if head.Val == target { @@ -79,27 +49,3 @@ func findNode(head *ListNode, target int) int { } return -1 } - -/* 双向链表结点结构体 */ -type DoublyListNode struct { - Val int // 结点值 - Next *DoublyListNode // 指向后继结点的指针(引用) - Prev *DoublyListNode // 指向前驱结点的指针(引用) -} - -// NewDoublyListNode 初始化 -func NewDoublyListNode(val int, nodes ...*DoublyListNode) *DoublyListNode { - var next, prev *DoublyListNode - length := len(nodes) - if length > 0 { - next = nodes[0] - } - if length > 1 { - prev = nodes[1] - } - return &DoublyListNode{ - Val: val, - Next: next, - Prev: prev, - } -} diff --git a/codes/go/chapter_array_and_linkedlist/linked_list_test.go b/codes/go/chapter_array_and_linkedlist/linked_list_test.go index 5deabff88..18061efff 100644 --- a/codes/go/chapter_array_and_linkedlist/linked_list_test.go +++ b/codes/go/chapter_array_and_linkedlist/linked_list_test.go @@ -6,42 +6,42 @@ package chapter_array_and_linkedlist import ( "fmt" + "github.com/krahets/hello-algo/pkg" "testing" ) func TestLikedList(t *testing.T) { /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ // 初始化各个结点 - n0 := NewListNode(1) - n1 := NewListNode(3) - n2 := NewListNode(2) - n3 := NewListNode(5) - n4 := NewListNode(4) + n0 := pkg.NewListNode(1) + n1 := pkg.NewListNode(3) + n2 := pkg.NewListNode(2) + n3 := pkg.NewListNode(5) + n4 := pkg.NewListNode(4) // 构建引用指向 n0.Next = n1 n1.Next = n2 n2.Next = n3 n3.Next = n4 + fmt.Println("初始化的链表为") + pkg.PrintLinkedList(n0) - fmt.Println("链表结构:") - PrintLinkedList(n0) + /* 插入结点 */ + insertNode(n0, pkg.NewListNode(0)) + fmt.Println("插入结点后的链表为") + pkg.PrintLinkedList(n0) - P := NewListNode(6) - - /* 在链表的结点 n0 之后插入结点 P */ - insertNode(n0, P) - fmt.Println("在链表的结点 n0 之后插入结点 P 后,链表结构:") - PrintLinkedList(n0) - - /* 删除链表的结点 n0 之后的首个结点 */ + /* 删除结点 */ removeNode(n0) - fmt.Println("删除链表的结点 n0 之后的首个结点后,链表结构:") - PrintLinkedList(n0) + fmt.Println("删除结点后的链表为") + pkg.PrintLinkedList(n0) - /* 访问链表中索引为 index 的结点 */ - fmt.Println("访问链表中索引为 2 的结点:", access(n0, 2)) + /* 访问结点 */ + node := access(n0, 3) + fmt.Println("链表中索引 3 处的结点的值 =", node) - /* 在链表中查找值为 target 的首个结点 */ - fmt.Println("在链表中查找 5 的首个节点索引值:", findNode(n0, 5)) + /* 查找结点 */ + index := findNode(n0, 2) + fmt.Println("链表中值为 2 的结点的索引 =", index) } From 679d5314d9df578d0555fba67b28bb3983f3c999 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 2 Jan 2023 18:41:21 +0800 Subject: [PATCH 4/5] Update linked_list.md --- .../linked_list.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 6627fb930..a55d68028 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -174,19 +174,19 @@ comments: true === "Go" ```go title="" - /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个结点 - n0 := NewListNode(1) - n1 := NewListNode(3) - n2 := NewListNode(2) - n3 := NewListNode(5) - n4 := NewListNode(4) - - // 构建引用指向 - n0.Next = n1 - n1.Next = n2 - n2.Next = n3 - n3.Next = n4 + /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ + // 初始化各个结点 + n0 := NewListNode(1) + n1 := NewListNode(3) + n2 := NewListNode(2) + n3 := NewListNode(5) + n4 := NewListNode(4) + + // 构建引用指向 + n0.Next = n1 + n1.Next = n2 + n2.Next = n3 + n3.Next = n4 ``` === "JavaScript" From bacf8553c5527d9f14c4093aa789c8f6c900ebdc Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Mon, 2 Jan 2023 18:42:58 +0800 Subject: [PATCH 5/5] Update linked_list.go --- codes/go/chapter_array_and_linkedlist/linked_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/go/chapter_array_and_linkedlist/linked_list.go b/codes/go/chapter_array_and_linkedlist/linked_list.go index e7fdb35ca..106e49f4c 100644 --- a/codes/go/chapter_array_and_linkedlist/linked_list.go +++ b/codes/go/chapter_array_and_linkedlist/linked_list.go @@ -5,7 +5,7 @@ package chapter_array_and_linkedlist import ( - "github.com/krahets/hello-algo/pkg" + . "github.com/krahets/hello-algo/pkg" ) /* 在链表的结点 n0 之后插入结点 P */