NASA666

不以物喜,不以己悲.
Posted on , viewed 595 times

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

题目描述:
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
代码实现:

// 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
package main
import (
    "fmt"
)

type Item struct{
    data int
    prex  *Item
    next  *Item
}

func main() {
    var head *Item
    var tail *Item
    var current *Item
    // 初始化
    for i := 0; i <= 10; i++ {
        t := &Item{i, nil, nil}
        if i == 0 {
            head = t
        } else {
            t.prex = current
            current.next = t
        }
        if i == 10 {
            tail = t
        }
        current = t
    }
    current = tail
    var s []int
    for current != nil {
        s = append(s, current.data)
        current = current.prex
    }
    fmt.Println(s)
    current = head
    for current != nil {
        fmt.Println(current)
        current = current.next
    }
}