题目描述
输入一个链表,从尾到头打印链表每个节点的值。
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> vt; while(head != NULL){ vt.push_back(head->val); head = head->next; } int size = vt.size(); int i,j=size - 1,tmp; for(i = 0; i<j ; i++,j--) { tmp = vt[i]; vt[i] = vt[j]; vt[j] = tmp; } return vt; } };