题目地址 https://www.acwing.com/problem/content/description/18/数组
来源:剑指Offerspa
输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值。code
返回的结果用数组存储。blog
输入:[2, 3, 5] 返回:[5, 3, 2]
题解:ip
将链表转换成vector 其实大量链表题目 若是容许的话 均可以转化成链表作 比较便利get
代码it
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: vector<int> printListReversingly(ListNode* head) { ListNode* p =head; vector<int> v; while(p != NULL){ v.push_back(p->val); p= p->next; } reverse(v.begin(),v.end()); return v; } };