/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
/**翻转单链表,打印
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> ret;
if (!head) return ret;
ret = printListFromTailToHead(head->next);
ret.push_back(head->val);
// ListNode* pre = nullptr;
// ListNode* cur = head;
// ListNode* next = head->next;
// while(cur ){
// next=cur->next;
// cur->next = pre;
// pre=cur;
// cur=next;
// }
// vector<int> ret;
// while (pre) {
// ret.push_back(pre->val);
// pre = pre->next;
// }
return ret;
}
};
剑指数据结构-从尾到头打印链表
最新推荐文章于 2024-05-21 17:24:14 发布