题目
输入一个链表,返回一个反序的链表。
代码实现
package main
import (
"fmt"
"strconv"
)
type LinkedList struct {
Value string
Next *LinkedList
}
func (ll LinkedList) prinfListInReserveOrder() {
if ll.Next != nil {
ll.Next.prinfListInReserveOrder()
}
fmt.Print("<- ", ll.Value)
}
func (ll LinkedList) prinfListInOrder() {
fmt.Print(" ->", ll.Value)
if ll.Next != nil {
ll.Next.prinfListInOrder()
}
}
func (ll *LinkedList) Push(v string) {
TempList := ll
for TempList.Next != nil {
TempList = TempList.Next
}
TempList.Next = &LinkedList{
Value: v,
}
}
func (ll *LinkedList) Set(v string) {
ll.Value = v
}
func sword3() {
ll := &LinkedList{
Value: "123",
}
for i := 0; i < 10; i++ {
ll.Push(strconv.Itoa(i))
}
fmt.Println("PinrfInOrder")
ll.prinfListInOrder()
fmt.Println("")
fmt.Println("PinrfInReservedOrder")
ll.prinfListInReserveOrder()
}
输出
F:/CodeArea/Exericese/go/go.exe [F:/CodeArea/Exericese/go] main start!!! PinrfInOrder ->123 ->0 ->1 ->2 ->3 ->4 ->5 ->6 ->7 ->8 ->9 PinrfInReservedOrder <- 9<- 8<- 7<- 6<- 5<- 4<- 3<- 2<- 1<- 0<- 123成功: 进程退出代码 0.