我无法创建长链接列表

package main

func main() {
    var arr [99]ListNode
    for i := 0; i < 99; i++ {
        var tempArr ListNode
        tempArr.Val = i
        arr[i] = tempArr
    }
    for i := 0; i < 98; i++ {
        arr[i].Next = &arr[i+1]
    }
}

type ListNode struct {
    Val  int
    Next *ListNode
}

I want to create a linked list with 99 elements, but it breaks every 3 node. See the image below for more information:

image

Your linked list is working fine although it's not the right way to create linked lists. The reason that you only 3 nodes is your debugger tracing limitations. I wrote a printing loop to show the values in the linked list.

package main

func main() {
    var arr [99]ListNode
    for i := 0; i < 99; i++ {
        var tempArr ListNode
        tempArr.Val = i
        arr[i] = tempArr
    }
    for i := 0; i < 98; i++ {
        arr[i].Next = &arr[i+1]
    }
}

type ListNode struct {
    Val  int
    Next *ListNode
}

Also I wrote another code to show how you can create linked lists in its proper way.

package main

import "fmt"

func main() {
    start := &ListNode{}
    node := start
    for i := 0; i < 100; i++ {
        node.Val = i
        node.Next = &ListNode{}
        node = node.Next
    }
    for node := start; node.Next != nil; node = node.Next {
        fmt.Printf("%d -> ", node.Val)
    }
}

type ListNode struct {
    Val  int
    Next *ListNode
}