题目

https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

思路

递归,归阶段push

解题

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val) {
  4. * this.val = val;
  5. * this.next = null;
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @return {number[]}
  11. */
  12. // 递归或者栈
  13. var reversePrint = function(head) {
  14. const result = []
  15. const reverse = (head, result) => {
  16. if (!head) {
  17. return
  18. }
  19. const prev = head
  20. head = head.next
  21. reverse(head, result)
  22. result.push(prev.val)
  23. }
  24. reverse(head, result)
  25. return result
  26. };