/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { int jiediangeshu = 0; ListNode* cur = head; vector<int> re; while (cur != NULL) { ++jiediangeshu; cur = cur->next; } for (int i = (jiediangeshu - 1); i >= 0; --i) { ListNode* temp = head; int x=i; while (x>0) { temp = temp->next; --x; } re.push_back(temp->val); } return re; } };
时间: 2024-11-13 04:11:16