package main import "fmt" type Node[T comparable] struct { Next *Node[T] Value T } func NewNode[T comparable](value T) *Node[T] { return &Node[T]{Value: value} } type List[T comparable] struct{ head *Node[T] tail *Node[T] length uint } func NewList[T comparable]() *List[T] { return &List[T]{} } func (l *List[T]) append(value T) { node := NewNode(value) if l.tail == nil { l.tail = node l.head = node } else { l.tail.Next = node l.tail = node } l.length++ } func (l *List[T]) print() { if l.head == nil { return } temp := l.head for temp != nil { fmt.Println(temp.Value) temp = temp.Next } } func (l *List[T]) reverse() { if l.head == nil { return } // swap head&tail temp := l.head l.head = l.tail l.tail = temp // reverse var before *Node[T] var after *Node[T] for temp != nil { after = temp.Next temp.Next = before before = temp temp = after } } func main() { list := NewList[int]() list.append(10) list.append(20) list.append(30) list.append(40) list.append(50) list.print() fmt.Println("--------------------") list.reverse() list.print() }