main.go 939 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. )
  9. func main() {
  10. input := bufio.NewScanner(os.Stdin)
  11. for input.Scan() {
  12. line := input.Text()
  13. var values = strings.Split(line, " ")
  14. var digits []int
  15. var num int
  16. for _, v := range values {
  17. num, _ = strconv.Atoi(v)
  18. digits = append(digits, num)
  19. }
  20. var head *ListNode
  21. var cur *ListNode = head
  22. for _, v := range digits {
  23. var tmp = &ListNode{Val: v, Next: nil}
  24. if head == nil {
  25. head = tmp
  26. cur = head
  27. continue
  28. }
  29. cur.Next = tmp
  30. cur = tmp
  31. }
  32. newHead := reverseList(head)
  33. for newHead != nil {
  34. fmt.Println(newHead.Val)
  35. newHead = newHead.Next
  36. }
  37. }
  38. }
  39. type ListNode struct {
  40. Val int
  41. Next *ListNode
  42. }
  43. func reverseList(head *ListNode) *ListNode {
  44. var newHead *ListNode
  45. for head != nil {
  46. var tmpNode = &ListNode{
  47. Val: head.Val,
  48. Next: newHead,
  49. }
  50. newHead = tmpNode
  51. head = head.Next
  52. }
  53. return newHead
  54. }