AcWing 17. 从尾到头打印链表
原题链接
简单
作者:
满地王八我壳最绿.
,
2023-12-03 16:28:54
,
所有人可见
,
阅读 44
/**
* 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> num;
int i=0;
ListNode * p=head;
while(p!=NULL){
num.push_back(p->val);//如果这个元素不存在,那就得用push_back()方法来添加一个新元素到容器末尾
//要注意vector 可变长数组的问题。
p=p->next;
}
reverse(num.begin(),num.end());//逆置数组.容器自带的
return num;
}
};