123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "strconv"
- "strings"
- )
- func main() {
- input := bufio.NewScanner(os.Stdin)
- for input.Scan() {
- line := input.Text()
- var values = strings.Split(line, " ")
- var digits []int
- var num int
- for _, v := range values {
- num, _ = strconv.Atoi(v)
- digits = append(digits, num)
- }
- var head *ListNode
- var cur *ListNode = head
- for _, v := range digits {
- var tmp = &ListNode{Val: v, Next: nil}
- if head == nil {
- head = tmp
- cur = head
- continue
- }
- cur.Next = tmp
- cur = tmp
- }
- newHead := reverseList(head)
- for newHead != nil {
- fmt.Println(newHead.Val)
- newHead = newHead.Next
- }
- }
- }
- type ListNode struct {
- Val int
- Next *ListNode
- }
- func reverseList(head *ListNode) *ListNode {
- var newHead *ListNode
- for head != nil {
- var tmpNode = &ListNode{
- Val: head.Val,
- Next: newHead,
- }
- newHead = tmpNode
- head = head.Next
- }
- return newHead
- }
|