题目链接:https://www.acwing.com/problem/content/18/
题目如下:
/**
* 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) {
vector<int> result;
//1、反转链表
ListNode* pre=NULL,*cur=head;
while(cur!=NULL){
ListNode* temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
//2、将内容存入result中
while(pre!=NULL){
result.push_back(pre->val);
pre=pre->next;
}
return result;
}
};