2 回答
TA贡献1829条经验 获得超6个赞
基本问题是您永远不会为结构指针分配内存。当你写:
var list *ListNode
您已经创建了一个指向 ListNode 类型的指针,但您实际上并没有为它分配任何内存。所以当你尝试写...
list.Val = i
您收到“无效内存地址”错误,因为您试图取消引用未定义的指针。分配内存的一种方法是使用new()新的内置函数:
var list *ListNode = new(ListNode)
您还可以获取结构的地址,如下所示:
list := &ListNode{}
上面显示了正确的语法,但是如果您只是用var上面的声明替换现有的声明,您的代码中仍然会有逻辑问题:您不想分配任何内存,直到您将第一个节点添加到列表中。这意味着我们要等到我们进入for循环内才能分配内存。
通过对您的代码进行一些小的更改,我们得到:
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func main() {
var head, tail *ListNode
for i := 0; i <= 5; i++ {
node := new(ListNode)
node.Val = i
if tail == nil {
// This is the first node in the list, so just point head
// and tail at the new node.
tail = node
head = tail
} else {
// There is at least one node in the list, so attach the new
// node to the tail
tail.Next = node
tail = node
}
}
result := head
for result != nil {
fmt.Printf("%d\t", result.Val)
// Don't forget to increment to the next node!
result = result.Next
}
}
运行此代码会产生:
0 1 2 3 4 5
TA贡献1871条经验 获得超13个赞
您必须为列表节点分配内存。创建列表节点时,更新前一个节点中的 Next 字段或更新列表头(如果这是第一个节点)。
var head *ListNode
// p is pointer to head or pointer to previous node's Next.
// We start with p set as pointer to the head.
p := &head
for i := 0; i <= 5; i++ {
// Allocate a new ListNode with Val initialized.
n := &ListNode{Val: i}
// Update head or previous node'a Next field.
*p = n
// The next iteration of the loop should update
// the Next field in the node that we just created.
p = &n.Next
}
// Loop while list node is not nil.
for n := head; n != nil; n = n.Next {
fmt.Println(n.Val)
}
https://go.dev/play/p/qUhza05kUFT
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报