1、遍历压栈,出栈打印,前进后出
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { ListNode * Node=head; stack<int> s; vector<int> result; if(head==NULL)//链表为空 return result; while(Node!=NULL)//循环到最后节点 { s.push(Node->val); Node=Node->next;//向后遍历 } while(!s.empty()) { result.push_back(s.top()); s.pop(); } return result; } };