[剑指Offer] 3.从尾到头打印链表

题目描述

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

【思路】用一个vector存储,遍历链表时每次从前面插入

 /**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> S;
ListNode* node = head;
while(node!=NULL){
S.insert(S.begin(),node->val);
node = node->next;
}
return S;
}
};
上一篇:[剑指offer]6.从尾到头打印链表+18.删除链表节点


下一篇:Java实现文件压缩与解压[zip格式,gzip格式]