题目描述

输入一个链表,从尾到头打印链表每个节点的值。

没什么难度,看清从尾到头即可...

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> re;
        while (head) {
            re.push_back(head->val);
            head = head->next;
        }
        for (int i = 0; i < re.size() / 2; i++) swap(re[i], re[re.size() - 1 - i]);
        return re;
    }
};

 

相关文章:

  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2022-02-13
  • 2021-11-16
  • 2021-09-09
  • 2021-06-17
  • 2021-12-28
  • 2021-11-02
  • 2022-01-28
相关资源
相似解决方案