1. type ListNode struct {
    2. Val int
    3. Next *ListNode
    4. }
    5. func reverselist(head *ListNode) *ListNode {
    6. var temp *ListNode
    7. pre := head
    8. for pre != nil {
    9. t := pre.Next
    10. pre.Next = temp
    11. temp = pre
    12. pre = t
    13. }
    14. return temp
    15. }
    16. type NodeTree struct {
    17. Val int
    18. Left *NodeTree
    19. Right *NodeTree
    20. }
    21. func levelOrder(node *NodeTree) (res [][]int) {
    22. res = [][]int{}
    23. if node == nil {
    24. return
    25. }
    26. queue := []*NodeTree{node}
    27. for len(queue) > 0 {
    28. //创建临时队列,保存层节点
    29. tmpQ := []*NodeTree{}
    30. tmpRes := []int{}
    31. for j := 0; j < len(queue); j++ {
    32. node := queue[j]
    33. tmpRes = append(tmpRes, node.Val)
    34. if node.Right != nil {
    35. tmpQ = append(tmpQ, node.Right)
    36. }
    37. if node.Left != nil {
    38. tmpQ = append(tmpQ, node.Left)
    39. }
    40. }
    41. //处理下一层
    42. queue = tmpQ
    43. res = append(res, tmpRes)
    44. }
    45. return
    46. }