从尾到头,可以通过栈临时存储:
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> list; while(head != NULL) { list.push_back(head->val); head = head->next; } reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组 return list; } };