package algorithm

import "fmt"

type Node struct {
	value int
	next  *Node
}

var head *Node = nil

func InitList(val int) {
	var cur *Node = nil
	var i int
	for i = 1; i <= val; i++ {
		node := new(Node)
		node.value = i
		node.next = nil

		if head == nil {
			head = node
			cur = node
		} else {
			cur.next = node
			cur = node
		}
	}
}

func PrintList() {
	// 打印链表
	fmt.Print("list:")
	node := head
	for {
		fmt.Printf("%d ", node.value)
		if node.next == nil {
			break
		}
		node = node.next
	}
	fmt.Println()
}

func ReverseList() {
	var header *Node
	var next *Node
	var pre *Node

	header = head
	next = nil
	pre = nil

	// 翻转列表
	for {
		if header == nil {
			break
		}

		next = header.next
		header.next = pre
		pre = header
		header = next
	}

	head = pre
}

func Test(size int) {
	InitList(size)
	PrintList()
	ReverseList()
	PrintList()
}
