之前写的,利用栈先进后出的原理,把链表压进栈,再打印就行了,非常简单。可能是为了之后的反转链表铺垫。
/** * 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) { stack<int> p; ListNode *b=head; while(b) { p.push(b->val); b=b->next; } vector<int> a; while(!p.empty()) { a.push_back(p.top()); p.pop(); } return a; } };