从尾到头打印链表
输入一个链表的头结点,按照 从尾到头 的顺序返回节点的值。
返回的结果用数组存储。
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> ans;
for (auto p = head; p; p = p->next) ans.push_back(p->val);
reverse (ans.begin(), ans.end());
return ans;
}
};

浙公网安备 33010602011771号