我的問題是,當我將head指向head.next時input.Val仍然是 1 而不是2(這是下一個值)。type ListNode struct { Val int Next *ListNode}func test(head *ListNode) *ListNode { head = head.Next return head}func main() { var input, input2 ListNode input = ListNode{Val: 1, Next: &input2}} input2 = ListNode{Val: 2} test(&input) fmt.Println(input.Val)}
1 回答

ABOUTYOU
TA貢獻1812條經驗 獲得超5個贊
這里是固定的:
package main
import (
? ? "fmt"
)
type ListNode struct {
? ? Val? int
? ? Next *ListNode
}
func test(head *ListNode) *ListNode {
? ? head = head.Next
? ? return head
}
func main() {
? ? var input1, input2 ListNode
? ? input1 = ListNode{Val: 1, Next: &input2}
? ? input2 = ListNode{Val: 2, Next: &input1}
? ? input := test(&input1)
? ? fmt.Println(input.Val)
}
產出
2
問題是您沒有使用函數的返回值test,并且傳遞了一個沒有值的節點Next。
- 1 回答
- 0 關注
- 101 瀏覽
添加回答
舉報
0/150
提交
取消