/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> ListV;
if(head == NULL)
{
return ListV;
}
//把list的值顺序转存到vector中
while (head->next != NULL)
{
ListV.push_back(head->val);
head = head->next;
}
ListV.push_back(head->val);
vector<int> reverseListV;
int a = ListV.size();
for (int i = 0; i < a; i++)
{
reverseListV.push_back(ListV[a - i - 1]);
}
return reverseListV;
}
};
从尾到头打印链表
最新推荐文章于 2021-01-26 19:34:12 发布